Code:
Private LastOpe As String
Private num As String
Private Sub Form_Load()
txtResult.Enabled = False
Me.KeyPreview = True
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' numbers in main keyboard
If (KeyCode >= 48 And KeyCode <= 57) Then
AddDigits (Chr(KeyCode))
End If
' numbers in numpad
If (KeyCode >= 96 And KeyCode <= 105) Then
AddDigits (Chr(KeyCode - 48))
End If
'if backspace
If (KeyCode = 8) Then
If (txtResult.Text <> "") Then
txtResult.Text = Left(txtResult.Text, Len(txtResult.Text) - 1)
End If
End If
'for various operators
If (KeyCode = 111) Then RegisterOperator ("/")
If (KeyCode = 106) Then RegisterOperator ("*")
If (KeyCode = 109) Then RegisterOperator ("-")
If (KeyCode = 107) Then RegisterOperator ("+")
End Sub
Private Sub cmdOperator_Click(Index As Integer)
RegisterOperator (cmdOperator(Index).Caption)
End Sub
Private Sub cmdClear_Click()
num = ""
LastOpe = ""
txtResult.Text = ""
End Sub
Private Sub cmdEquals_Click()
Select Case LastOpe
Case Is = "+"
txtResult.Text = num+ txtResult.Text
Case Is = "-"
txtResult.Text = num - txtResult.Text
Case Is = "/"
txtResult.Text = num / txtResult.Text
Case Is = "*"
txtResult.Text =num * txtResult.Text
End Select
num = ""
LastOpe = ""
End Sub
Private Sub Command1_Click(Index As Integer)
AddDigits (Command1(Index).Caption)
End Sub
Private Sub AddDigits(Digit As String)
txtResult.Text = txtResult.Text & Digit
End Sub
Private Sub RegisterOperator(OperatorText As String)
LastOpe = OperatorText
num = txtResult.Text
txtResult.Text = ""
End Sub
Bookmarks