Whenever the user clicks on one of the keys on the calculator’s keypad, the KeyButton_Click event will occur. Listing 13.6 shows how I handle this event.
Listing 13.6: KeyButton_Click Event in Programmable Calculator
Private Sub KeyButton_Click(Index As Integer)
On Error Resume Next
CurrentButton = Index
StatusBar1.Panels(1).Text = “”
If Toolbar1.Buttons.Item(“EditCode”).Value = tbrPressed Then
CodeBlock.Text = Code(Index)
CodeNam.Text = KeyButton(Index).Caption
KeyPos.Text = CStr(Index)
SSTab1.Tab = 1
Else
If KeyButton(Index).Caption <> “___” Then
ScriptControl1.Error.Clear
ScriptControl1.Run CodeName(Index)
End If
End If
End Sub
I start this routine by using the On Error Resume Next to prevent runtime errors from ending the program. Next, I save the Index of the currently pressed button and clear any messages that may be in the status bar.
Then I determine which mode the program is in by looking at the Edit Code button on the toolbar. If it’s pressed, then I get the code for the key button and display it in the CodeBlock text box on the Code View tab. I save the name of the subroutine in the CodeNam text box, and save the index of the button in the KeyPos text box. Then I display the Code View tab (SSTab1.Tab = 1).