Manual de Visual Basic Intermedio

 

  Lección: 7

 

Capítulo: 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

 

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

 

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

 

   Volver al índice!