| Topic: Menu-Driven and Calculator Projects In this lesson we will consider two projects, one a menu driven drawing project and the other a calculator project. Menus are one of the most important and most characteristic elements of the Windows User Interface. Menu Commands are similar to Controls. They recognize only the click events. When you group controls as arrays, give them the same name property, but give their index property as 0, 1, 2, 3 etc. to distinguish them.
A view of the Menu bar and some Menu Items: Suppose we want to build a program with a menu bar with two titles and one or more menu items for each of the menu titles somewhat as shown in the figure 1. In this example the menu items denote different pixel values for drawing lines. The different menu items are connected to appropriate codes which will enable you to select the pixel thickness from the menu and draw figures of of different thickness.
Menu Editor Window: Start VB from the Standard EXE mode. Select the item “Menu Editor” from the Tools Menu. The Menu Editor Window appears as shown in the figure 2. This window lets you create a menu bar that contains all the menu items you want.
Creating the Menu Bar: In this project we will have only two menu titles in the menu bar. Of course one can have as many as required for the concerned problem. The first title is ‘Draw’. Type ‘&Draw’ in the Caption field. The & symbol before D tells that D is a hot key and users can use alt+D key combination to pull down the menu. Next press the Tab key. This moves the insertion point to the next text box ‘Name’, which is the name of the control. Enter ‘menDraw’ in the Name field and click the right arrow key at the bottom of the menu editor window. This is how the computer recognizes the item as menu title. The prefix ‘men’ is used to indicate to the users that the item ‘Draw’ is the name of the menu title. The Caption field is cleared to enable the next menu title to be entered. Now enter ‘&Quit’ in the Caption field and ‘menQuit’ in the Name field and click OK. Now the design window will show a menu bar with two items ‘Draw’ and ‘Quit’. At the bottom you will see the two items entered as shown in the figure 2a. &Draw Figure 2a Creating Menu Items: Now we have to enter the menu items under the menu titles Draw and Quit. B Open the Menu Editor Window. Select &Quit. Select Insert. This action will insert a line between the two items. Type &Erase in the Caption field and miErase in the Name field. The prefix ‘mi’ indicates that the item ‘Erase’ is a menu item and not a menu title. Now the bottom window will appear as shown in the figure 2b. &Draw Figure 2b In a similar manner add the menu item Exit for the menu title Quit. Use the Caption name as E&xit and the control name as miExit. Adding Control Arrays: Next we have to add the three line-width menu items. The first one will have the Caption &1 Pixel and a control name ‘miPixel’ When different controls have the same array name they must be identified by indexes. So after entering the control name, move the insertion point to the index field and enter 1 for the first Pixel Control. For the subsequent Pixel Controls enter the index values as 2 and 3 respectively. Inserting Lines in MenusAfter highlighting the item next to the place where you want to insert a line, select insert button. Type a hyphen(-) symbol for the Caption name and miLine1 for the Control name. For subsequent lines give the control names as muLine2, miLine3 etc. Finally the design window will appear as shown in the figure 3. Insertion or deletion of lines and menu titles and menu items can be done with the help of insert, delete and OK buttons. Entering Codes for the Controls and Pop-up Menu: When you select any control, the code window for the particular control will be opened. Enter the codes for the different controls as shown in the figure 3a. A pop-up menu is the same as the other menus, except that it does not drop down from the menu bar, but displayed on the form at the place where you right-click the mouse. The code for creating the pop-up menu is also shown in the figure 3a. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Private Sub Form_Paint() Private Sub miErase_Click() Private Sub miexit_Click() Private Sub miPixel_Click(Index As Integer) Private Sub miPrint_Click() Running the Project: Select Start from the Run Menu. Then by clicking the menu items draw the word hello for three different thickness. The final run mode window will look as shown in the figure 4.
2. Calculator Project: The basic layout of the screen is shown in the figure 5. There are two control arrays of buttons one for the digits 0 to 9 and another for operators +, -, /, * and =. The form is given the name ‘calculator’ and its icon property is set to calculator icon. The display label is given the name ‘Readout’ and the caption ‘0.’. The number buttons 0 to 9 are given the same name ‘Number’, their captions and indexes are set as 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 respectively. The operator buttons /, +, X, - and = are given the same name, “Operator” , and their indexes are set as 0, 1, 2, 3 and 4 respy. The ‘%’ button is given the name ‘Percent’, the ‘.’ button is given the name ‘Decimal’, the ‘C’ button is given the name ‘Cancel’ and the ‘CE’ button is given the name ‘CancelEntry’. The codes are entered in the code window as shown in the figure 5a. Save and run the project and check that the calculator functions properly.Option Explicit ' Initialization routine for the form. Figure 5a
|