martes, 10 de junio de 2008

Visual Basic I - Clase VI



TAREA 3:
Hacer dos calculadoras con la apariencia vista en clases.


RESOLUCION

Calculadora 1:

Private Sub Form_Initialize()
TxtV1.Enabled = False
TxtV2.Enabled = False
LstOp.Enabled = False
TxtResult.Enabled = False
CmdOut.Enabled = False
TxtV1.Text = ""
TxtV2.Text = ""
TxtResult.Text = ""
End Sub

Private Sub CmdNew_Click()
TxtV1.Enabled = True
TxtV1.SetFocus
TxtV2.Enabled = True
TxtV1.Text = ""
TxtV2.Text = ""
TxtResult.Text = ""
CmdOut.Enabled = False
CmdNew.Enabled = False
End Sub

Private Sub TxtV1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub ' Esto es por si presiona el BackSpace
If KeyAscii <48 Or KeyAscii> 57 Then ' Esto no permite presionar una tecla que no sea un numero
KeyAscii = 0
End If
End Sub

Private Sub TxtV1_Validate(Cancel As Boolean)
If TxtV1.Text = "" Then
MsgBox ("Por favor, escriba un valor.")
Cancel = True
End If
End Sub

Private Sub TxtV2_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If KeyAscii <48 Or KeyAscii> 57 Then
KeyAscii = 0
End If
End Sub

Private Sub TxtV2_Validate(Cancel As Boolean)
If TxtV2.Text = "" Then
MsgBox ("Por favor, escriba un valor.")
Cancel = True
End If
End Sub

Private Sub TxtV2_LostFocus()
LstOp.Enabled = True
LstOp.SetFocus
End Sub

Private Sub LstOp_Click()
CmdNew.Enabled = True
CmdOut.Enabled = True
CmdNew.SetFocus
If LstOp.Text = "Suma" Then
TxtResult.Text = Val(TxtV1.Text) + TxtV2.Text
ElseIf LstOp.Text = "Resta" Then
TxtResult.Text = Val(TxtV1.Text) - TxtV2.Text
ElseIf LstOp.Text = "Multiplicación" Then
TxtResult.Text = Val(TxtV1.Text) * TxtV2.Text
ElseIf LstOp.Text = "División" And TxtV1.Text <> 0 And TxtV2.Text <> 0 Then
TxtResult.Text = Val(TxtV1.Text) / TxtV2.Text
ElseIf LstOp.Text = "División" And TxtV1.Text = 0 And TxtV2.Text = 0 Then
MsgBox ("Esta división no se puede realizar.")
End If
End Sub

Private Sub CmdOut_Click()
Unload Me
End Sub


Calculadora 2:

Dim Seleccion As Integer

Private Sub Form_Initialize()
CmdNew.Enabled = True
TxtV1.Enabled = False
TxtV2.Enabled = False
TxtResult.Enabled = False
CmdCalcular.Enabled = False
CmdOut.Enabled = False
OptSuma.Enabled = False
OptResta.Enabled = False
OptMultiplicacion.Enabled = False
OptDivision.Enabled = False
End Sub

Private Sub CmdNew_Click()
CmdNew.Enabled = False
CmdOut.Enabled = False
TxtV1.Enabled = True
TxtV2.Enabled = True
TxtV1.Text = ""
TxtV2.Text = ""
TxtResult.Text = ""
TxtV1.SetFocus
OptSuma.Value = False
OptResta.Value = False
OptMultiplicacion.Value = False
OptDivision.Value = False
End Sub

Private Sub TxtV1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub ' Esto es Por si presiona el BackSpace
If KeyAscii <48 Or KeyAscii> 57 Then ' Si presiono una tecla que no es un numero
KeyAscii = 0
End If
End Sub

Private Sub TxtV1_Validate(Cancel As Boolean)
If TxtV1.Text = "" Then
MsgBox ("Por favor, escriba un valor.")
Cancel = True
End If
End Sub

Private Sub TxtV2_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If KeyAscii "<"48 Or KeyAscii">" 57 Then
KeyAscii = 0
End If
End Sub

Private Sub TxtV2_Validate(Cancel As Boolean)
If TxtV2.Text = "" Then
MsgBox ("Por favor, escriba un valor.")
Cancel = True
End If
End Sub

Private Sub TxtV2_LostFocus()
OptSuma.Enabled = True
OptResta.Enabled = True
OptMultiplicacion.Enabled = True
OptDivision.Enabled = True
CmdCalcular.Enabled = True
OptSuma.SetFocus
TxtV1.Enabled = False
TxtV2.Enabled = False
End Sub

Private Sub CmdCalcular_Click()
If OptSuma.Value = True Then
Seleccion = 1
ElseIf OptResta.Value = True Then
Seleccion = 2
ElseIf OptMultiplicacion.Value = True Then
Seleccion = 3
ElseIf OptDivision.Value = True Then
Seleccion = 4
End If
Select Case Seleccion
Case 1
TxtResult.Text = Val(TxtV1.Text) + TxtV2.Text
Case 2
TxtResult.Text = TxtV1.Text - TxtV2.Text
Case 3
TxtResult.Text = TxtV1.Text * TxtV2.Text
Case 4
If OptDivision.Value = True And TxtV1.Text <> 0 And TxtV2.Text <> 0 Then
TxtResult.Text = TxtV1.Text / TxtV2.Text
ElseIf OptDivision.Value = True And TxtV1.Text = 0 And TxtV2.Text = 0 Then
MsgBox ("Esta división no se puede realizar.")
End If
End Select
CmdCalcular.Enabled = False
CmdNew.Enabled = True
CmdOut.Enabled = True
CmdNew.SetFocus
End Sub

Private Sub CmdOut_Click()
End
End Sub


Si alguna parte del código no es entendible entonces pregunten. Solo como aclaración:

En la calculadora 1: FrmCalculadora1 se refiere al nombre del formulario. TxtV1, TxtV2 y TxtResult son las tres casillas de texto. LstOp es la ComboBox donde se elige la operación. CmdNew y CmdOut son los botones de Nuevo y Salir respectivamente. Y en la vista de diseño del programa (osea cuando aparecen los objetos, no el código), se van a la ventana de propiedades de la ComboBox y en Style seleccionan DropDown List.

Para la calculadora 2: FrmCalculadora2 se refiere al nombre del formulario. TxtV1, TxtV2 y TxtResult son las casillas de texto. OptSuma, OptResta, OptMultiplicación y OptDivisión son las opciones que se presentan a la derecha de la pantalla. CmdCalcular, CmdNew y CmdOut son los botones de Calcular, Nuevo y Salir respectivamente.

Si quieren poner imagen en los botones entonces con un boton seleccionado se van a la ventana de propiedades del botón y ahí buscan donde dice Style y lo cambian a Graphical, con eso pueden poner una imagen cualquiera o cambiar el color del fondo del botón.

El código que puse ya está hecho bajo todas las condiciones que el maestro pidió:

1. No se permiten divisiones de 0 entre 0. Aparece un MsgBox cuando eso se intenta.
2. En ninguna casilla de texto(TextBox) se permite digitar letras ni signos, SOLO números.
3. El botón Resultado(CmdResult) siempre está deshabilitado.
4. El botón Nuevo(CmdNew) borra todo.
5. El cursor siempre empieza ubicado en la primera casilla.
6. No se pueden hacer operaciones con números negativos ni decimales.

Los dos códigos cumplen con esas condiciones como les repito. Cualquier duda comentar esta entrada.

FIN DE CLASE VI

1 comentario:

Anónimo dijo...

Gracias por el código. Ya lo estoy ejecutando, todo bien.