Designing a Calculator

Visual Studio 6.0

 As another example of using the ScriptControl, I developed a really programmable calculator—and that’s what I called the program, as you can see in Figure 13.11. Every button triggers a VBScript routine. You can edit these routines and change the button captions to make the calculator do whatever you want. You can also save your code into a disk file and load it back again. The only problem is that you will need to do all of the work one keystroke at a time.

Figure 13.11: The Really Programmable Calculator program

The basic design of this program is relatively simple. The form contains a large display for the digits and a control array of command buttons that the user can click. Each of the command buttons calls a VBScript subroutine to perform the appropriate function.

The program’s views are contained in an SSTab control with three tabs:

  • The first tab holds the Keyboard View, which contains the display window and the calculator’s keypad (see Figure 13.11).

  • The second tab holds the Code View, which is used to display and edit the VBScript routines, as shown in Figure 13.12.

  • The third tab shows the Register View, which holds some text boxes that can be used in intermediate calculations, as shown in Figure 13.13.

Figure 13.12: The Code View tab

Figure 13.13: The Register View tab

Besides the three views, the calculator also includes a toolbar with a status bar beneath it. The toolbar is used to control some of the higher-level functions, such as editing the code assigned to a key, saving and loading the entire VBScript program, and shutting down the program. The status bar displays various error messages that may occur while the calculator is running.

The calculator has two modes: the normal mode and the edit mode. When the Edit Code button is not pressed, the calculator works as you would expect. However, when the Edit Code button is pressed, clicking on any of the keypad buttons will immediately switch to the Code View tab and show the code associated with that key. The user can then edit the code and click the Apply button to accept the changed code. Normally, clicking the Apply button will switch back to the Keyboard View. However, if there is a syntax error, the Code View display will remain, the error message will be displayed in the status bar, and the cursor will be positioned at the error. Clicking the Cancel button will discard any changes and return the user to the Keyboard View.

© 1998 SYBEX Inc. All rights reserved.

Show: