Código calculadora simple para Visual Basic
Codigo:
Dim numero1, numero2, resultado, memoria As Double
Dim operador As Integer
Dim operactivo As Boolean
Dim decactivo As Boolean
Dim igualactivo As Boolean
Private Sub btn_decimal_Click()
If operactivo = True Then
lbl_pantalla.Caption = ""
lbl_pantalla.Caption = "0."
operactivo = False
Else
If decactivo = True Then
lbl_pantalla.Caption = lbl_pantalla.Caption
Else
lbl_pantalla.Caption = lbl_pantalla.Caption & "."
decactivo = True
End If
End If
End Sub
Private Sub btn_limpiar_Click()
lbl_pantalla.Caption = "0"
decactivo = False
operactivo = False
igualactivo = False
operador = 0
For x = 0 To 9
btn_numero(x).Enabled = True
Next x
For y = 1 To 4
btn_operador(y).Enabled = True
Next y
End Sub
Private Sub btn_numero_Click(Index As Integer)
If operactivo = True Then
lbl_pantalla.Caption = ""
lbl_pantalla.Caption = Index
operactivo = False
Else
If lbl_pantalla.Caption = "0" Then
lbl_pantalla.Caption = ""
lbl_pantalla.Caption = Index
Else
lbl_pantalla.Caption = lbl_pantalla.Caption & Index
End If
End If
End Sub
Private Sub btn_operador_Click(Index As Integer)
If operador = 0 Then
numero1 = Val(lbl_pantalla.Caption)
operador = Index
operactivo = True
Else
If igualactivo = True Then
numero1 = Val(lbl_pantalla.Caption)
operador = Index
operactivo = True
igualactivo = False
Else
If operactivo = True Then
If operador = Index Then
Else
numero1 = Val(lbl_pantalla.Caption)
operador = Index
operactivo = True
End If
Else
btn_resultado_Click
numero1 = resultado
operador = Index
operactivo = True
End If
End If
End If
igualactivo = False
End Sub
Private Sub btn_resultado_Click()
numero2 = Val(lbl_pantalla.Caption)
igualactivo = True
Select Case operador
Case 0
resultado = numero2
operactivo = True
decactivo = False
operador = 0
Case 1
resultado = numero1 + numero2
lbl_pantalla.Caption = resultado
operactivo = True
decactivo = False
operador = 1
Case 2
resultado = numero1 - numero2
lbl_pantalla.Caption = resultado
operactivo = True
decactivo = False
operador = 2
Case 3
resultado = numero1 * numero2
lbl_pantalla.Caption = resultado
operactivo = True
decactivo = False
operador = 3
Case 4
If numero2 = 0 Then
lbl_pantalla.Caption = "ERROR"
For x = 0 To 9
btn_numero(x).Enabled = False
Next x
For y = 1 To 4
btn_operador(y).Enabled = False
Next y
Else
resultado = numero1 / numero2
lbl_pantalla.Caption = resultado
operactivo = True
decactivo = False
operador = 3
End If
End Select
End Sub
Private Sub Form_Load()
lbl_pantalla.Caption = 0
numero1 = 0
numero2 = 0
memoria = 0
resultado = 0
operador = 0
operactivo = False
decactivo = False
igualactivo = False
End Sub

Comentarios
Publicar un comentario