Você está na página 1de 31

mailxmail - Cursos para compartir lo que sabes

Manual de Visual Basic


Intermedio
Autor: Jorge Alvarez

1
mailxmail - Cursos para compartir lo que sabes

Presentación del curso


Esta es la segunda parte del Manual de Visual Basic Principiante. Con él podrás
superar el nivel que adquiriste al realizar el primero y, por lo tanto, conseguir
trabajar con este programa con total autonomía.

2
mailxmail - Cursos para compartir lo que sabes

1. Ejercicio1: Aprende a Sumar

Crea un formulario con cinco etiquetas, una caja de texto, tres botones, dos
imágenes y tres líneas. Modifica las propiedades de las etiquetas de las cantidades y
la de la palabra de resultado como sigue: BackColor blanco y Border Style 1-Fixed
Single. Usa Font y ForeColor para el tamaño y color del contenido y Alignment
2-Center
En Word crea las caritas y colócalas dentro de los cuadros de imágenes con su
propiedad Stretch en True, las líneas hazlas más gruesas en BorderWidth 3 y
BorderColor azul.
Vamos a crear un procedimiento para generar al azar los números:
Haz doble clic en el formulario. Haz clic en Tools (Herramientas)/Add Procedure
(Agregar Procedimiento). En Add Procedure escribe el nombre del procedimiento, en
este caso Azar y haz clic en Aceptar. Ahora escribe el código:
Public Sub azar()
n1 = Int(Rnd * 10) + 1
n2 = Int(Rnd * 10) + 1
Label1.Caption = n1
Label2.Caption = n2
End Sub

Haz doble clic en el formulario y escribe el siguiente código:


Private Sub Form_Load()
Randomize
Call azar
Image1.Visible = False
Image2.Visible = False
End Sub

3
mailxmail - Cursos para compartir lo que sabes

El botón Otra Suma


Private Sub Command1_Click()
Call azar
Text1.Text = ""
Label3.Caption = ""
Image1.Visible = False
Image2.Visible = False
End Sub

El botón Revisar
Private Sub Command2_Click()
If Val(Text1) = Val(Label1) + Val(Label2) Then
Label3.Caption = "Bien"
Image1.Visible = True
Else
Label3.Caption = "Mal"
Image1.Visible = False
Image2.Visible = True
End If
End Sub

El botón Salir
Private Sub Command3_Click()
Unload Me
End
End Sub

4
mailxmail - Cursos para compartir lo que sabes

2. Ejercicio 2: Ordenar los Números

Crea un formulario con nueve etiquetas, dos de ellas serán las instrucciones y a las
que contendrán los números (seis) cámbiales su Propiedad Alignment a 2-Center,
BackColor blanco, BorderStyle a 1-Fixed Single y tres botones, ahora
Introduce las siguientes declaraciones
Haz doble clic en el formulario En la caja de combo de la esquina superior izquierda
selecciona (General) Y escribe: Dim n1, n2, n3 As Integer (presiona Enter al terminar)
Ahora crea un procedimiento:
Haz doble clic en el formulario. Haz clic en Herramientas (Tools)/Agregar
Procedimiento (Add Procedure). En Agregar Procedimiento escribe el nombre del
Procedimiento (en este caso azar) y haz clic en Aceptar. Ahora escribe el siguiente
código:
Public Sub azar()
n1 = Int(Rnd * 10) + 1
n2 = Int(Rnd * 10) + 1
n3 = Int(Rnd * 10) + 1
If n1 <> n2 And n2 <> n3 And n1 <> n3 Then
Label1.Caption = n1
Label2.Caption = n2
Label3.Caption = n3
Else
Exit Sub
End If
End Sub
La etiqueta 4
Private Sub Label4_DragDrop(Source As Control, X As Single, Y As Single)
Label4.Caption = Source
Source.Visible = False
End Sub

La etiqueta 5

5
mailxmail - Cursos para compartir lo que sabes
Private Sub Label5_DragDrop(Source As Control, X As Single, Y As Single)
Label5.Caption = Source
Source.Visible = False
End Sub

La etiqueta 6
Private Sub Label6_DragDrop(Source As Control, X As Single, Y As Single)
Label6.Caption = Source
Source.Visible = False
End Sub

El botón Iniciar
Private Sub Command1_Click()
Label4.Caption = ""
Label5.Caption = ""
Label6.Caption = ""
Label7.Caption = ""
Label1.Visible = True
Label2.Visible = True
Label3.Visible = True
Call azar
End Sub

El botón Resultado
Private Sub Command2_Click()
If Val(Label4.Caption) < Val(Label5.Caption) And Val(Label5.Caption) <
Val(Label6.Caption) Then
Label7.Caption = "Bien"
Else
Label7.Caption = "Mal"
End If
End Sub

El botón Salir

6
mailxmail - Cursos para compartir lo que sabes
Private Sub Command3_Click()
Unload Me
End
End Sub

El formulario
Private Sub Form_Load()
Randomize
Call azar
End Sub

7
mailxmail - Cursos para compartir lo que sabes

3. Ejercicio 3: Calcular la edad

Crea un formulario con dos etiquetas, dos cajas de texto y tres botones, escribe el
siguiente código:
El botón Calcular la Edad
Private Sub Command1_Click()
Dim fecnac As Date, edad As Integer
fecnac = CDate(Text1)
edad = CInt((Date - fecnac) / 365)
Text2 = Str(edad) & " años"
End Sub

El botón Nuevo Cálculo


Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()
Unload Me
End
End Sub

8
mailxmail - Cursos para compartir lo que sabes

4. Ejercicio 4: Cálculo de Ventas

Crea un formulario con ocho etiquetas, ocho cajas de texto, un control Microsoft
Hierarchical FlexGrid (haz clic con el botón derecho del ratón sobre la caja de
herramientas y haz clic en Componentes, selecciona la casilla Microsoft Hierarchical
FlexGrid Control 6.0 y haz clic en Aceptar) tres botones y escribe el siguiente código:
Doble clic sobre el formulario
Private Sub Form_Load()
grdmenu.Cols = 2
grdmenu.Rows = 8
grdmenu.FixedCols = 0
grdmenu.FixedRows = 1
grdmenu.TextArray(0) = "Menú"
grdmenu.TextArray(1) = "Precio"
grdmenu.TextArray(2) = "Burrito"
grdmenu.TextArray(3) = "9.50"
grdmenu.TextArray(4) = "Burger"
grdmenu.TextArray(5) = "12.75"
grdmenu.TextArray(6) = "Torta"
grdmenu.TextArray(7) = "8.25"
grdmenu.TextArray(8) = "Refresco"
grdmenu.TextArray(9) = "4.00"
grdmenu.TextArray(10) = "Cerveza"
grdmenu.TextArray(11) = "15.60"
grdmenu.TextArray(12) = "Ensalada"
grdmenu.TextArray(13) = "14.45"

9
mailxmail - Cursos para compartir lo que sabes

grdmenu.TextArray(14) = "Hot Dog"


grdmenu.TextArray(15) = "6.90"
End Sub

El botón Aceptar
Private Sub Command1_Click()
Dim burrito As Integer, burger As Integer
Dim torta As Integer, refresco As Integer
Dim cerveza As Integer, ensalada As Integer
Dim hotdog As Integer, ventatotal As Double
burrito = Val(Text1)
burger = Val(Text2)
torta = Val(Text3)
refresco = Val(Text4)
cerveza = Val(Text5)
ensalada = Val(Text6)
hotdog = Val(Text7)
ventatotal = burrito * 9.5 + burger * 12.75 + torta * 8.25 + refresco * 4.60 +
cerveza * 15.6 + ensalada * 14.45 + hotdog * 6.9
Text8 = Str(ventatotal)
End Sub

El botón Limpiar
Private Sub Command2_Click()
Text1 = "": Text2 = ""
Text3 = "": Text4 = ""
Text5 = "": Text6 = ""
Text7 = "": Text8 = ""
text9 = "": Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()

10
mailxmail - Cursos para compartir lo que sabes
Unload Me
End
End Sub

11
mailxmail - Cursos para compartir lo que sabes

5. Ejercicio 5: Fecha Escrita

Crea un formulario con cinco etiquetas, tres cajas de texto, un marco, tres botones y
escribe el siguiente código:
El botón Convertir
Private Sub Command1_Click()
Dim cadmes As String, mes As Integer
mes = Val(Text2)
Select Case mes
Case 1: cadmes = "Enero"
Case 2: cadmes = "Febrero"
Case 3: cadmes = "Marzo"
Case 4: cadmes = "Abril"
Case 5: cadmes = "Mayo"
Case 6: cadmes = "Junio"
Case 7: cadmes = "Julio"
Case 8: cadmes = "Agosto"
Case 9: cadmes = "Septiembre"
Case 10: cadmes = "Octubre"
Case 11: cadmes = "Noviembre"
Case 12: cadmes = "Diciembre"
Case Else
MsgBox "Revísalos Por Favor", vbCritical, "Hay un Error en los Datos...!!!"
Call Command2_Click
Exit Sub

12
mailxmail - Cursos para compartir lo que sabes
Exit Sub
End Select
Label4 = Text1 & " de " & cadmes & " de " & Text3
End Sub

El botón Limpiar
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()
If MsgBox("¿Ya Quieres Salir del Programa?", vbQuestion + vbYesNo, "Tengo una
Pregunta para Ti") = vbYes Then
End
Else: Call Command2_Click
End If
End Sub

13
mailxmail - Cursos para compartir lo que sabes

6. Ejercicio 6: Análisis Numérico

Crea un formulario con cuatro marcos, siete etiquetas, ocho cajas de texto, tres
botones y escribe el siguiente código:
El botón Análisis
Private Sub Command1_Click()
If IsNumeric(Text1) Then
Dim s As Integer, si As Integer, sp As Integer
Dim may As Integer, min As Integer
Dim cad As String
Dim i As Integer, j As Integer
n = CLng(Text1)
m = CLng(Text1)
cad = ""
i = 0
j = 1
s = sp = si = 0
For j = 1 To n
If (n Mod j = 0) Then
cad = cad & j & vbCrLf
End If
Next j

14
mailxmail - Cursos para compartir lo que sabes

While (n > 0)
If ((n Mod 10) Mod 2) = 0 Then
sp = sp + (n Mod 10)
Else
si = si + (n Mod 10)
End If
s = s + (n Mod 10)
n = n \ 10
i = i + 1
Wend
may = Mid(Text1, 1, 1)
men = may
While (m > o)
If may < (m Mod 10) Then
may = m Mod 10
End If
If men > (m Mod 10) Then
men = m Mod 10
End If
m = m \ 10
Wend
Text2 = Str(i)
Text3 = Str(si)
Text4 = Str(sp)
Text5 = Str(s)
Text6 = Str(may)
Text7 = Str(men)
Text8 = cad
Else
MsgBox "Debes Introducir un Número, El que Quieras pero Introduce Un Número",
vbCritical, "Aviso Importante"

15
mailxmail - Cursos para compartir lo que sabes
Text1.SetFocus
End If
End Sub

El botón Otro Número


Private Sub Command2_Click()
Text1 = "": Text2 = ""
Text3 = "": Text4 = ""
Text5 = "": Text6 = ""
Text7 = "": Text8 = ""
Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()
If MsgBox("¿Deseas Cerrar el Programa?", vbQuestion + vbYesNo, "Aviso Antes de
Cerrar el Programa") = vbYes Then
End
Else
Cancel = True
Text1.SetFocus
End If
End Sub

16
mailxmail - Cursos para compartir lo que sabes

7. Ejercicio 7: Lista de Números

Crea un formulario con tres marcos, una caja de texto, dos botones de opción, una
caja de lista, cuatro botones y escribe el siguiente código:
Haz doble clic en el formulario y escribe:
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("¿Cerramos el Programa?", vbQuestion + vbYesNo, "¿Ya nos Vamos?") =
vbYes Then
End
Else: Cancel = True: Text1.SetFocus
End If
End Sub

El botón Agregar
Private Sub Command1_Click()
If IsNumeric(Text1.Text) Then
List1.AddItem Text1.Text
Text1.Text = ""
Text1.SetFocus
Else
MsgBox "Introduce un Número", vbCritical, "Por Favor"
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Text1.SetFocus
End If
End Sub

17
mailxmail - Cursos para compartir lo que sabes

El botón Ordenar
Private Sub Command2_Click()
Dim i As Integer, j As Integer, t As Integer, n As Integer
Dim a() As Integer
n = List1.ListCount
ReDim a(n)
For i = 0 To n - 1
a(i) = List1.List(i)
Next i
If Option1.Value Then
For i = 0 To n - 2
For j = i + 1 To n - 1
If a(i) > a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
End If
Text1.SetFocus
If Option2.Value Then
For i = 0 To n - 2
For j = i + 1 To n - 1
If a(i) < a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i

18
mailxmail - Cursos para compartir lo que sabes

End If
List1.Clear
For i = 0 To n - 1
List1.List(i) = a(i)
Next i
Text1.SetFocus
End Sub

El botón Lista Nueva


Private Sub Command4_Click()
Text1 = ""
List1.Clear
Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()
If MsgBox("¿Seguro que Quieres Cerrar el Programa?", vbQuestion + vbYesNo, "¿Ya
es Hora de Irnos?") = vbYes Then
End
Else: Cancel = True: Text1.SetFocus
End If
End Sub

19
mailxmail - Cursos para compartir lo que sabes

8. Ejercicio 8: Registro de Cursos

Crea un formulario con cuatro marcos, seis etiquetas, cinco cajas de texto, un
cuadro combinado, dos cajas de lista, cuatro botones y escribe el siguiente código:
Private Sub Form_Load()
List1.AddItem "Intro. a Sistemas"
List1.AddItem "Windows"
List1.AddItem "MS Word"
List1.AddItem "MS Excel"
List1.AddItem "MS PowerPoint"
List1.AddItem "MS Access"
List1.AddItem "Soporte Técnico"
List1.AddItem "Redes"
List1.AddItem "MS Visual Basic"
List1.AddItem "Internet"
List2.AddItem "Chihuahua"
List2.AddItem "Campeche"
List2.AddItem "Veracruz"
List2.AddItem "Colima"
List2.AddItem "Yucatán"
List2.AddItem "Sonora"
List2.AddItem "Sinaloa"

20
mailxmail - Cursos para compartir lo que sabes
List2.AddItem "Puebla"
List2.AddItem "Tabasco"
List2.AddItem "Zacatecas"
Combo1.AddItem "Masculino"
Combo1.AddItem "Femenino"
End Sub

Private Sub Form_Unload(Cancel As Integer)


If MsgBox("¿Estás Seguro de Querer Cerrar el Programa?", vbQuestion + vbYesNo,
"¿Oye qué pasa contigo?") = vbYes Then
End
Else
Cancel = True
End If
End Sub

Private Sub List1_Click()


Text1 = List1
End Sub

Private Sub List2_Click()


Text5 = List2
End Sub

El botón Guardar
Private Sub Command1_Click()
List1.Enabled = False
List2.Enabled = False
Text1.Locked = True
Text2.Locked = True
Combo1.Locked = True
Text4.Locked = True
Text5.Locked = True
MsgBox "Este Alumno ha sido Matriculado", vbInformation, "Mensaje Importante"

21
mailxmail - Cursos para compartir lo que sabes
End Sub

El botón Limpiar
Private Sub Command2_Click()
List1.Enabled = True
List2.Enabled = True
Text1.Locked = False
Text2.Locked = False
Text3.Locked = False
Combo1.Locked = False
Text4.Locked = False
Text5.Locked = False
Text1 = ""
Text2 = ""
Text3 = ""
Combo1 = ""
Text4 = ""
Text5 = ""
Text1.SetFocus
End Sub

El botón Cancelar
Private Sub Command3_Click()
If MsgBox("Deseas Cancelar Algún Dato?", vbQuestion + vbYesNo, "Aviso Muy
Importante") = vbYes Then
List1.Enabled = True
List2.Enabled = True
Text1.Locked = False
Text2.Locked = False
Text3.Locked = False
Combo1.Locked = False
Text4.Locked = False
Text5.Locked = False

22
mailxmail - Cursos para compartir lo que sabes
End If
End Sub

El botón Salir
Private Sub Command4_Click()
Unload Me
End
End Sub

23
mailxmail - Cursos para compartir lo que sabes

9. Ejerciocio 9: Palíndromos

Crea un formulario con tres etiquetas, dos cajas de texto, tres botones y escribe el
siguiente código:
Function cadinvertida(cadena As String) As String
Dim invertida() As String * 1
Dim i As Integer
Dim j As Integer
n = Len(cadena)
ReDim invertida(n)
For i = 1 To n
invertida(i - 1) = Mid(cadena, i, 1)
Next i
For j = (n - 1) To 0 Step -1
cadinvertida = cadinvertida & invertida(j)
Next j
End Function

El botón Invertir
Private Sub Command1_Click()
Text2 = cadinvertida(Text1)
End Sub

El botón Limpiar
Private Sub Command2_Click()
Text1 = ""

24
mailxmail - Cursos para compartir lo que sabes

Text2 = ""
Text1.SetFocus
End Sub

El botón Salir
Private Sub Command3_Click()
Unload Me
End
End Sub

25
mailxmail - Cursos para compartir lo que sabes

10. Ejercicio 10: Inter Compras Net

Crea un formulario con siete etiquetas, cinco cuadros image, un botón y escribe el
siguiente código:
Dim total As Double
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Move X, Y
End Sub

El botón Cuenta Nueva


Private Sub Command1_Click()
Label7 = ""
total = o
End Sub

Private Sub Image5_DragDrop(Source As Control, X As Single, Y As Single)


If Source = Image1 Then
total = total + 250#
ElseIf Source = Image2 Then
total = total + 375#
ElseIf Source = Image3 Then
total = total + 400#
ElseIf Source = Image4 Then
total = total + 500#
End If

26
mailxmail - Cursos para compartir lo que sabes
End If
Label7 = "$" & total & ".00"
End Sub

27
mailxmail - Cursos para compartir lo que sabes

11. Ejercicio 11: Calificaciones

Crea un formulario con ocho etiquetas, nueve cajas de texto, tres botones ye escribe
el siguiente código:
El botón Salir
Private Sub Command3_Click()
Unload Me
End
End Sub

El botón Limpiar
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text9 = ""
Text1.SetFocus
End Sub

El botón Prom. Gral. Redondeado

28
mailxmail - Cursos para compartir lo que sabes

Private Sub Command1_Click()


Dim n1 As Double, n2 As Double, n3 As Double, n4 As Double, n5 As Double, n6 As
Double
Dim promedio As Integer
n1 = Val(Text2): n2 = Val(Text3)
n3 = Val(Text4): n4 = Val(Text5)
n5 = Val(Text6): n6 = Val(Text7)
promedio = ((n1 + n2 + n3 + n4 + n5 + n6) / 6)
Text8 = Str(promedio)
If promedio >= 9.5 Then
Text9 = "Excelente"
End If
If promedio >= 9 And promedio < 9.5 Then
Text9 = "Muy Bien"
End If
If promedio >= 8.5 And promedio < 9 Then
Text9 = "Bien"
End If
If promedio >= 8 And promedio < 8.5 Then
Text9 = "Regular"
End If
If promedio < 8 Then
Text9 = "Mal"
End If
End Sub

29
mailxmail - Cursos para compartir lo que sabes

12. Ejercicio 12: Impresión

Crea un formulario con seis etiquetas, seis cajas de texto, dos botones y escribe el
siguiente código:
El botón Limpiar
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text1.SetFocus
End Sub

El botón Imprimir
Private Sub Command1_Click()
Printer.Orientation = 1
Printer.FontSize = 12
Printer.Print
Printer.Print
Printer.Print
Printer.Print Tab(15); Label1.Caption; Tab(30); Text1.Text
Printer.Print
Printer.Print

30
mailxmail - Cursos para compartir lo que sabes

Printer.Print Tab(15); Label2.Caption; Tab(30); Text2.Text


Printer.Print
Printer.Print
Printer.Print Tab(15); Label3.Caption; Tab(30); Text3.Text
Printer.Print
Printer.Print
Printer.Print Tab(15); Label4.Caption; Tab(30); Text4.Text
Printer.Print
Printer.Print
Printer.Print Tab(15); Label5.Caption; Tab(30); Text5.Text
Printer.Print
Printer.Print
Printer.Print Tab(15); Label6.Caption; Tab(30); Text6.Text
Printer.EndDoc
End Sub

31

Você também pode gostar