Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Connecting Menus to Event Procedures

After you've built your menu, you need to tie each menu command to your application. To respond to menu selections, you need to write Click() event procedures that you want Visual Basic to execute when the user selects a menu command.

Visual Basic generates a Click event when the user selects a menu command. The name of the menu command, combined with Click(), provides the name of the event procedure. Therefore, the File | Exit menu item named mnuFileExit will generate the execution of the event procedure named mnuFileExit_Click().

Adding the mnuFileExit_Click() event procedure requires only that you select that menu command during the program's development. At the Form window, click the File menu bar command. Visual Basic displays the Form window's File pull-down menu. Even though you're not running the program but are working on the program from the Form window, the File menu shows you what happens when the user selects File at runtime.

Click the Exit item on the File pull-down menu. As soon as you click Exit, Visual Basic opens the Code window to a new event procedure named mnuFileExit_Click(), as shown in Figure 17.9.

17fig09.gif

Figure 17.9 The menu option's Click() event procedure.

This event procedure is simple to code. When the user selects File | Exit, you want the application to terminate. Therefore, insert an Unload Me and an End statement to the body of the mnuFileExit_Click() procedure and close the procedure by double-clicking its control button. As you can see, adding event procedures requires little more than clicking the menu item and adding the body of the procedure that appears.

Although the application is far from complete, you can run the application to see how the menu looks and to test the File | Exit option.

After building your menu, you must tie code to the various menu items by writing Click() event procedures that will execute when the user runs the application and selects from the menu. If any menu command duplicates the functionality of other controls, such as command buttons, don't copy the command button's code into the body of the menu event procedure. Instead, simply execute that command button's event procedure from the menu item's event procedure.

Share ThisShare This

Informit Network