0% found this document useful (0 votes)
67 views28 pages

Programacion I Visual Basic PDF

The document contains examples of Visual Studio code for calculating various mathematical formulas using buttons and text boxes. It includes 6 examples that calculate the area of a triangle, potential energy, work, addition/subtraction/multiplication/division/exponentiation of numbers, area of a cylinder, and converting between radians and degrees. The last example demonstrates using an IF-THEN-ELSE conditional structure to calculate either the area or perimeter of a parabola based on a radio button selection.

Uploaded by

walter quintero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views28 pages

Programacion I Visual Basic PDF

The document contains examples of Visual Studio code for calculating various mathematical formulas using buttons and text boxes. It includes 6 examples that calculate the area of a triangle, potential energy, work, addition/subtraction/multiplication/division/exponentiation of numbers, area of a cylinder, and converting between radians and degrees. The last example demonstrates using an IF-THEN-ELSE conditional structure to calculate either the area or perimeter of a parabola based on a radio button selection.

Uploaded by

walter quintero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Ejemplos de Visual Studio

EJERCICIO # 1

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: Area de triángulo
'Fecha:
'Declaración de variables
Dim A, B, H As Single
B = TextBox1.Text
H = TextBox2.Text
'Calculando área de triángulo
A = (B * H) / 2
'Visualizar resultado
TextBox3.Text = A
End Sub
_________________________________________________________________________________

1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


End

End Sub

End Class
__________________________________________________________________________________

EJERCICIO # 2

2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Energía potencial
'Fecha:

'Declaración de variables
Dim ep, m, x As Single
m = TextBox1.Text
x = TextBox2.Text

'Calcular energía potencial


ep = m * 9.8 * x

'Visualizar resultado
TextBox3.Text = ep
End Sub
_________________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


End
End Sub

_________________________________________________________________________________

EJERCICIO # 3

3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa:Trabajo
'Fecha:
'Declaración de variables
Dim w, m, vf, vi As Single
m = TextBox1.Text
vf = TextBox2.Text
vi = TextBox3.Text
'Calcular el trabajo
w = (m * (vf ^ 2) / 2) - (m * (vi ^ 2) / 2)
'visualizar resultado en cada de mensaje
MsgBox("El trabajo calculado es: " & w)
End Sub
_________________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________________

4
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End

End Sub
_________________________________________________________________________________

EJERCICIO # 4

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
c=a+b
TextBox3.Text = c
End Sub
_________________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Dim a, b, c As Single

5
a = TextBox1.Text
b = TextBox2.Text
c=a-b
TextBox3.Text = c
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
c=a*b
TextBox3.Text = c
End Sub
_________________________________________________________________________________

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click


Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
c=a/b
TextBox3.Text = c
End Sub
_________________________________________________________________________________

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click


Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
c=a^b
TextBox3.Text = c
End Sub
_________________________________________________________________________________

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click


TextBox1.Text = " "
TextBox2.Text = " "

6
TextBox3.Text = " "
End Sub
_________________________________________________________________________________

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click


End
End Sub
End Class

_________________________________________________________________________________

EJERCICIO # 5

7
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Area de cilindro
'Fecha:

'Declaración de variables
Dim a, r, h As Single
r = TextBox1.Text
h = TextBox2.Text

'Calculando área de cilindro


a = 2 * 3.141592 * (r + h + r ^ 2)

'Visualizando resultado
TextBox3.Text = a

End Sub
_________________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub

8
_________________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End

End Sub
_________________________________________________________________________________

EJERCICIO # 6

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: Conversión a radianes
'Fecha:
'Declaración de variables
Dim rad, dato As Single
dato = TextBox1.Text
'Calculando radianes
rad = (2 * 3.141592 * dato) / 360
'Visualizar resultado en radianes
TextBox2.Text = rad
Label3.Text = "Radianes"
End Sub
_________________________________________________________________________________

9
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Declaración de variables
Dim grad, dato As Single
dato = TextBox1.Text
'Calculando grados
grad = (dato * 180)
'Visualizar resultado en grados
TextBox2.Text = grad
Label3.Text = "Grados"
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


TextBox1.Text = " "
TextBox2.Text = " "
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click


End
End Sub
_________________________________________________________________________________

USO DE CICLO CONDICIONAL IF-THEN-ELSE


IF-THEN-ELSE: Es un ciclo condicional que permite tomar decisiones dentro de un programa, se utiliza para evaluar condiciones y que el
programa pueda operar sobre dos alternativas.

EJERCICIO # 7

10
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: área y perímetro de parábola
'Fecha
'Declaración de variables
Dim are, p, a, b As Single
a = TextBox1.Text
b = TextBox2.Text
If RadioButton1.Checked = True Then
are = 3.141592 * a * b
MsgBox("El área de la parábola es : " & are)
Else
If RadioButton2.Checked = True Then
p = 2 * 3.141592 * (1 / 2 * (a ^ 2 + b ^ 2)) ^ 0.5
MsgBox("El perímetro de la parábola es : " & p)
End If
End If
End Sub
_________________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

11
End
End Sub
_________________________________________________________________________________

EJERCICIO # 8

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: Discriminante
'Declaración de variables
Dim d, b, a, c, r As Single
b = TextBox1.Text
a = TextBox2.Text
c = TextBox3.Text
'Calculando cantidad subradical
r = (b ^ 2 - 4 * a * c)
'Evaluando si se puede calcular raíz cuadrada
If r > 0 Then
d = r ^ 0.5
Else
If r < 0 Then
MsgBox("No se puede calcular la raíz cuadrada")
End If
End If
'Visualizar resultado

12
TextBox4.Text = d
End Sub
______________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
End Sub
_______________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________________

EJERCICIO # 9

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: Calculadora_If-Then-Else
'Declaración de variables
Dim r, a, b As Single
a = TextBox1.Text
b = TextBox2.Text
'Ciclo condicional para tomar decisiones y poder elegir operación
If RadioButton1.Checked = True Then

13
r=a+b
Else
If RadioButton2.Checked = True Then
r=a-b
Else
If RadioButton3.Checked = True Then
r=a*b
Else
If RadioButton4.Checked = True Then
r=a/b
Else
If RadioButton5.Checked = True Then
r=a^b
End If
End If End If End If
End If
MsgBox("El resultado es : " & r)
End Sub
_____________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________________

EJERCICIO # 10

14
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles ComboBox1.Selected
IndexChanged
'Programa: Conceptos
'Uso de ciclo condicional IF-THEN-ELSE
If ComboBox1.Text = "Aceleración" Then
Label1.Text = "La aceleración es el cambio de velocidad en el tiempo para desplazar un cuerpo de un punto A a un punto B."
Label3.Text = "a = (Vf -Vo) / t"
Label2.Text = "Ingrese velocidad final (Vf):"
Label4.Text = "Ingrese velocidad inicial (Vo):"
Label5.Text = "Ingrese el tiempo ( t ):"
Label6.Text = "La aceleración es ( a ):"
Else
If ComboBox1.Text = "Velocidad inicial (Vo)" Then
Label1.Text = "Es la Velocidad que tiene un cuerpo al INICIAR su movimiento en un período de tiempo."
Label3.Text = "Vo = Vf - a t"
Label2.Text = "Ingrese velocidad final (Vf):"
Label4.Text = "Ingrese aceleración ( a ):"
Label5.Text = "Ingrese el tiempo ( t ):"
Label6.Text = "La velocidad inicial es (Vo):"

15
Else
If ComboBox1.Text = "Velocidad final (Vf)" Then
Label1.Text = "Es la Velocidad que tiene un cuerpo al FINALIZAR su movimiento en un período de tiempo."
Label3.Text = "Vf = Vo + a t"
Label2.Text = "Ingrese velocidad inicial (Vo):"
Label4.Text = "Ingrese aceleración ( a ):"
Label5.Text = "Ingrese el tiempo ( t ):"
Label6.Text = "La velocidad final es (Vf):"
End If
End If
End If
End Sub
_________________________________________________________________________________

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


If ComboBox1.Text = "Aceleración" Then
Dim a, vf, vo, t As Single
vf = TextBox1.Text
vo = TextBox2.Text
t = TextBox3.Text
a = (vf - Vo) / t
TextBox4.Text = a
Else
If ComboBox1.Text = "Velocidad inicial (Vo)" Then
Dim vo, vf, a, t As Single
vf = TextBox1.Text
a = TextBox2.Text
t = TextBox3.Text
vo = vf - (a * t)
TextBox4.Text = vo
Else
If ComboBox1.Text = "Velocidad final (Vf)" Then
Dim vf, vo, a, t As Single
vo = TextBox1.Text
a = TextBox2.Text
t = TextBox3.Text
vf = vo + (a * t)

16
TextBox4.Text = vf
End If
End If
End If
End Sub
_________________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
End Sub
_________________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
End Class
_________________________________________________________________________________

EJERCICIO # 11

17
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Funciones trigonométricas
'Declaración de variables
Dim r, ang As Single
ang = TextBox1.Text
'Evaluando condición, para seleccionar función
If RadioButton1.Checked = True Then
r = Math.Cos(ang)
Else
If RadioButton2.Checked = True Then
r = Math.Sin(ang)
Else
If RadioButton3.Checked = True Then
r = Math.Tan(ang)
End If
End If
End If
TextBox2.Text = r
End Sub
_________________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


End
End Sub
_________________________________________________________________________________

EJERCICIO # 12

18
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Área de circulo
'Fecha: 14 de Agosto de 2012

'Declaración de variables
Dim a, r As Single
r = TextBox1.Text

'Calcular operación área


a = 3.141592 * r ^ 2

'Visualizar resultado
TextBox2.Text = a
End Sub
________________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

19
End
End Sub
_________________________________________________________________________________

EJERCICIO # 13

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: calculo de hipotenusa
'Declaración de variables
Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
'Realizar cálculo de hipotenusa
c = (a ^ 2 + b ^ 2) ^ 0.5
'Visualizar cálculo de hipotenusa
TextBox3.Text = c
End Sub

20
_______________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub

________________________________________________________________________

EJERCICIO # 14

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


'Programa: Distancia entre dos puntos
'Declaración de variables

21
Dim x2, x1, y2, y1, d As Single
x2 = TextBox1.Text
x1 = TextBox2.Text
y2 = TextBox3.Text
y1 = TextBox4.Text
'Calculo de distancia
d = ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5
'Visualizar resuldo
TextBox5.Text = d
End Sub
______________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
End Sub
_______________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub

_______________________________________________________________________
EJERCICIO # 15

22
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Ciclo condicional promedio
Dim nomb As String
Dim estado As String
Dim p1, p2, p3, p4, prom As Single
nomb = TextBox1.Text
p1 = TextBox2.Text
p2 = TextBox3.Text
p3 = TextBox4.Text
p4 = TextBox5.Text
prom = (p1 + p2 + p3 + p4) / 4
'Ciclo condicional para evaluar el estado
If prom >= 7 Then
estado = "Aprobado"

23
Else
If prom < 7 Then
estado = "R e p r o b a d o"
End If
End If
MsgBox(nomb & "su promedio es:" & prom & "Su estado actual:" & estado)

End Sub
________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
End Sub

________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


End
End Sub
_______________________________________________________________________
EJERCICIO # 16

Estructura Select - Case

24
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Programa: Estructura de control Select Case (Meses)
Dim numeromes As String
numeromes = TextBox1.Text
Select Case numeromes
Case "1"
TextBox2.Text = "Enero"
Case "2"
TextBox2.Text = "Febrero"
Case "3"
TextBox2.Text = "Marzo"
Case "4"
TextBox2.Text = "Abril"
Case "5"
TextBox2.Text = "Mayo"
Case "6"
TextBox2.Text = "Junio"
Case "7"
TextBox2.Text = "Julio"
Case "8"

25
TextBox2.Text = "Agosto"
Case "9"
TextBox2.Text = "Septiembre"
Case "10"
TextBox2.Text = "Octubre"
Case "11"
TextBox2.Text = "Noviembre"
Case "12"
TextBox2.Text = "Diciembre"
Case Else
TextBox2.Text = "Digite dato correctamente"
End Select
End Sub
________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
End Sub
________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub

________________________________________________________________________

EJERCICIO # 17

26
Formulario usando variables y caja de mensaje

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim num1, num2, suma As Single
num1 = 100
num2 = 200
suma = num1 + num2
MsgBox("La suma de " & num1 & " y " & num2 & " es " & suma)
End Sub
________________________________________________________________________
EJERCICIO # 18
Código para cambiar el color a un formulario

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim RGB1, RGB2, RGB3 As Integer
RGB1 = TextBox1.Text

27
RGB2 = TextBox2.Text
RGB3 = TextBox3.Text
Me.BackColor = Color.FromArgb(RGB1, RGB2, RGB3)
End Sub

________________________________________________________________________

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub

________________________________________________________________________

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


End
End Sub

28

You might also like