Calculator

 

For VBtoday, I am showing you the codes of the calculator.

 

 

 

Option Explicit
Dim Num_of_digit As Integer
Dim key As Integer
Dim displayValue As Variant
Dim a, b, c, d, e, f, g As Variant
Dim memo As Variant
Dim newNumber As Boolean






Private Sub BZero_Click(Index As Integer)
If Num_of_digit > 0 Then
panel.Caption = panel.Caption + "0"
Else
panel.Caption = "0"
Num_of_digit = Num_of_digit + 1
End If
CheckValue
End Sub

Private Sub Form_Load()

Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2

End Sub



Sub CheckValue()
displayValue = Val(panel.Caption)

End Sub

Private Sub ButtonNum_Click(Index As Integer)

If Num_of_digit > 0 Then
If Num_of_digit < 30 Then
panel.Caption = panel.Caption + Right$(Str$(Index), 1)

Num_of_digit = Num_of_digit + 1
End If

Else
panel.Caption = Right$(Str$(Index), 1)
Num_of_digit = 1
End If

CheckValue
End Sub

Private Sub Clear_Click()

panel.Caption = "0"
displayValue = "0"
Num_of_digit = 0

End Sub


Private Sub ClearAll_Click()

panel.Caption = "0"
displayValue = "0"
memo = 0

End Sub


Private Sub Equal_Click()

CheckValue
If newNumber = True Then

If key = 1 Then
e = displayValue + a
ElseIf key = 2 Then
e = b - displayValue

ElseIf key = 3 Then
e = displayValue * c

ElseIf key = 5 Then
e = (f * displayValue) / 100

ElseIf key = 4 And displayValue <> 0 Then
e = d / displayValue

Else

GoTo error

End If
If Abs(e) < 1 Then
panel.Caption = Format(e, "General Number")
Else

panel.Caption = Str(e)
End If
Else
panel.Caption = displayValue

End If

GoTo finish

error: panel.Caption = "E"

finish:

Num_of_digit = 0
newNumber = False



End Sub


Private Sub MemoCancel_Click()
memo = 0
End Sub



Private Sub Memory_Click()
CheckValue
memo = displayValue
Num_of_digit = 0
End Sub

Private Sub Operator_Click(Index As Integer)


CheckValue
If Index = 11 Then
a = displayValue
key = 1

ElseIf Index = 12 Then
b = displayValue
key = 2

ElseIf Index = 13 Then
c = displayValue
key = 3

ElseIf Index = 14 Then
d = displayValue
key = 4

ElseIf Index = 15 Then
f = displayValue
key = 5

End If
Num_of_digit = 0

newNumber = True

End Sub





Private Sub Percent_Click(Index As Integer)

End Sub

Private Sub Plus_minus_Click()
CheckValue
g = -1 * displayValue
displayValue = g
panel.Caption = Str$(displayValue)
CheckValue


End Sub


End Sub

Private Sub Poin_Click()
Static point_lock As Integer
If point_lock = 0 And Num_of_digit < 20 Then
panel.Caption = panel.Caption + "."
Num_of_digit = Num_of_digit + 1
End If
CheckValue

End Sub

Private Sub Recall_Click()
panel.Caption = Str$(memo)

End Sub

Private Sub SqRoot_Click()
CheckValue
If displayValue >= 0 Then
panel.Caption = Str$(Sqr(displayValue))
Else
panel.Caption = "E"
End If

Num_of_digit = 0

End Sub

Private Sub Summation_Click()
CheckValue

memo = memo + displayValue

Num_of_digit = 0

End Sub