Está en la página 1de 28

Ejemplos de Visual Basic 2008

EJEMPLO # 1

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button1.Click
'Programa: Area de tringulo
'Compilador: Visual Basic 2008
'Programador: Medinilla
'Fecha:
'Declaracin de variables
Dim A, B, H As Single
B = TextBox1.Text
H = TextBox2.Text
'Calculando rea de tringulo
A = (B * H) / 2
'Visualizar 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
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
End
End Sub
End Class
Lic. Edgar de la Cruz Garca.

EJEMPLO

# 2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Energa potencial
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha: 30 de Julio de 2012
'Declaracin de variables
Dim ep, m, x As Single
m = TextBox1.Text
x = TextBox2.Text
'Calcular energa 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 Syste
m.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As Syste
m.EventArgs) Handles Button3.Click
End
End Sub
__________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa:Trabajo
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha: 30 de Julio de 2012
'Declaracin 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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = "
"
TextBox2.Text = "
"
TextBox3.Text = "
"
End Sub
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 4

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste
m.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 S
ystem.EventArgs) Handles Button2.Click
Dim a, b, c As Single
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 Syste
m.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 Syste
m.EventArgs) Handles Button4.Click
Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
c = a / b
Lic. Edgar de la Cruz Garca.

TextBox3.Text = c
End Sub
_________________________________________________________________________
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As Syste
m.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 S
ystem.EventArgs) Handles Button6.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button7.Click
End
End Sub
End Class

Lic. Edgar de la Cruz Garca.

EJEMPLO # 5

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Area de cilindro
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha:
'Declaracin 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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 6

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Conversin a radianes
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha: 7/08/12
'Declaracin 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
_________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As Syste
m.EventArgs) Handles Button2.Click
'Declaracin 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 Syste
m.EventArgs) Handles Button3.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
Lic. Edgar de la Cruz Garca.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As S


ystem.EventArgs) Handles Button4.Click
End
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

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.
EJEMPLO # 7

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: rea y permetro de parbola
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha
'Declaracin 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 parbola es : " & are)
Else
If RadioButton2.Checked = True Then
p = 2 * 3.141592 * (1 / 2 * (a ^ 2 + b ^ 2)) ^ 0.5
MsgBox("El permetro de la parbola es : " & p)
End If
End If
End Sub
_________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________
Lic. Edgar de la Cruz Garca.

EJEMPLO # 8

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Discriminante
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha: 7 /08/12
'Declaracin 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 raz cuadrada
If r > 0 Then
d = r ^ 0.5
Else
If r < 0 Then
MsgBox("No se puede calcular la raz cuadrada")
End If
End If
'Visualizar resultado
TextBox4.Text = d
End Sub
_________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = " "
Lic. Edgar de la Cruz Garca.

TextBox2.Text = " "


TextBox3.Text = " "
TextBox4.Text = " "
End Sub
_________________________________________________________________________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 9

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Calculadora_If-Then-Else
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha:
'Declaracin de variables
Dim r, a, b As Single
a = TextBox1.Text
b = TextBox2.Text
'Ciclo condicional para tomar decisiones y poder elegir operacin
If RadioButton1.Checked = True Then
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 S
ystem.EventArgs) Handles Button2.Click
Lic. Edgar de la Cruz Garca.

TextBox1.Text = " "


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

Lic. Edgar de la Cruz Garca.

EJEMPLO # 10

Public Class Form1


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Obj
ect, ByVal e As System.EventArgs)Handles ComboBox1.SelectedIndexChanged
'Programa: Conceptos
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha:
'Uso de ciclo condicional IF-THEN-ELSE
If ComboBox1.Text = "Aceleracin" Then
Label1.Text = "La aceleracin 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 aceleracin 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 perodo de tiempo."
Label3.Text = "Vo = Vf - a t"
Label2.Text = "Ingrese velocidad final (Vf):"
Label4.Text = "Ingrese aceleracin ( a ):"
Label5.Text = "Ingrese el tiempo ( t ):"
Label6.Text = "La velocidad inicial es (Vo):"
Else
If ComboBox1.Text = "Velocidad final (Vf)" Then
Label1.Text = "Es la Velocidad que tiene un cuerpo al
FINALIZAR su movimiento en un perodo de tiempo."
Label3.Text = "Vf = Vo + a t"
Label2.Text = "Ingrese velocidad inicial (Vo):"
Label4.Text = "Ingrese aceleracin ( a ):"
Label5.Text = "Ingrese el tiempo ( t ):"
Label6.Text = "La velocidad final es (Vf):"
Lic. Edgar de la Cruz Garca.

End If
End If
End If
End Sub
_________________________________________________________________________
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button1.Click
If ComboBox1.Text = "Aceleracin" 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)
TextBox4.Text = vf
End If
End If
End If
End Sub
_________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As S
ystem.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 S
ystem.EventArgs) Handles Button3.Click
End
End Sub
End Class
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 11

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Funciones trigonomtricas
'Compilador: Visual Basic 2008
'Programadora: Medinilla
'Fecha:
'Declaracin de variables
Dim r, ang As Single
ang = TextBox1.Text
'Evaluando condicin, para seleccionar funcin
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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S


ystem.EventArgs) Handles Button3.Click
End
End Sub

Lic. Edgar de la Cruz Garca.

EJEMPLO # 12

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: rea de circulo
'Compilador: Visual Basic
'Programadora: Medinilla
'Fecha: 14 de Agosto de 2012
'Declaracin de variables
Dim a, r As Single
r = TextBox1.Text
'Calcular operacin 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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
_________________________________________________________________________
________
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As S
ystem.EventArgs) Handles Button3.Click
End
End Sub
_________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 13

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: calculo de hipotenusa
'Compilador: Visual Basic 2008
'Fecha:
'Programador: Medinilla
'Declaracin de variables
Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text
'Realizar clculo de hipotenusa
c = (a ^ 2 + b ^ 2) ^ 0.5
'Visualizar clculo de hipotenusa
TextBox3.Text = c
End Sub
________________________________________________________________________
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As Syste
m.EventArgs) Handles Button2.Click
TextBox1.Text = "

"

TextBox2.Text = "

"

TextBox3.Text = "

"

End Sub
Lic. Edgar de la Cruz Garca.

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

Lic. Edgar de la Cruz Garca.

EJEMPLO # 14

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Distancia entre dos puntos
'Compilador: Visual Basic 2008
'Fecha:
'Programador: Medinilla
'Declaracin de variables
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 Syste
m.EventArgs) Handles Button2.Click
TextBox1.Text = "

"

TextBox2.Text = "

"
Lic. Edgar de la Cruz Garca.

TextBox3.Text = "

"

TextBox4.Text = "

"

TextBox5.Text = "

"

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

Lic. Edgar de la Cruz Garca.

EJEMPLO # 15

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Ciclo condicional promedio
'Compilador: Visual Basic 2008
'Fecha:
'Programador: Medinilla
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"
Else
If prom < 7 Then
Lic. Edgar de la Cruz Garca.

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 Syste
m.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 S
ystem.EventArgs) Handles Button3.Click
End
End Sub

________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 16
Estructura Select - Case

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
'Programa: Estructura de control Select Case (Meses)
'Compilador: Visual Basic 2008
'Fecha:
'Programador: Medinilla
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"
Lic. Edgar de la Cruz Garca.

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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = "

"

TextBox2.Text = "

"

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

Lic. Edgar de la Cruz Garca.

EJEMPLO # 17
Formulario usando variables y caja de mensaje

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.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

Ejecucin

________________________________________________________________________

Lic. Edgar de la Cruz Garca.

EJEMPLO # 18
Cdigo para cambiar el color a

un formulario

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste


m.EventArgs) Handles Button1.Click
Dim RGB1, RGB2, RGB3 As Integer
RGB1 = TextBox1.Text
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 S
ystem.EventArgs) Handles Button2.Click
TextBox1.Text = "

"

TextBox2.Text = "

"

TextBox3.Text = "

"

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

Lic. Edgar de la Cruz Garca.

También podría gustarte