Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Adding Menus To Your Visual Basic Apps

One of the many features that most Windows applications have in common is a menu system. Visual Basic’s Menu Editor makes adding menus to your programs a snap—and these menus are every bit as functional as the ones you see in any Windows program. You can add menus to any Visual Basic form; in a multiple-form program, each form can have its own menus. A menu is just another control, and like other controls, it has properties and responds to events. Because of the special requirements of menus, however, you must design a menu and set its properties with the Menu Editor.

To start the Menu Editor (shown in Figure 11.2), display a form and press Ctrl+E or select Menu Editor from the Tools menu. You can also click on the Menu Editor button on the toolbar. Rather than bore you with a long-winded description of how the editor works, I’ll walk you through the creation of the menus for our Baby Editor. Once we have completed that, you’ll know most of what there is to know about the Menu Editor and menu design. The rest I can fill in quickly and easily.

Creating The Text Editor’s Menu

What will our editor need in the way of menu commands? To keep the project simple, we’ll create only two menus: a File menu and an Edit menu. The File menu will contain the New, Open, Save, Save As, and Exit commands. The Edit menu will contain the Copy, Cut, Paste, and Font commands. Let’s get to work.


Figure 11.2  The Visual Basic Menu Editor.

Display the project’s form, then press Ctrl+E or select Tools|Menu Editor to display the Menu Editor. Because this form does not yet have a menu, the editor is blank. We will start by adding the first menu command to display on the menu bar. Because we’re following Windows conventions, this will be the File menu. In the Caption box, enter “&File” and you’ll see the menu caption appear in the large box at the bottom of the dialog box. Next, tab to the Name box and enter “mnuFile”, then click on the Next button. The highlight will move down a line in the menu outline, and the Text Boxes will be cleared, ready for you to enter the next menu item.

First, let me point out two things:

  Menu items can have access keys. In the menu displayed in the program, an access key is designated by the underlined letter in the menu command. The user can quickly access the menu command by pressing that key when the menu is displayed. As with other control captions, you specify the access key by preceding it with &.
  In addition, each menu item has a Name property. As with other types of controls, the Name property is used in the event procedures associated with the item. I have developed the habit of beginning all menu item names with mnu, then completing the name with the menu caption(s). Thus, the File menu is named mnuFile, and the Open command on the File menu will be named mnuFileOpen. This naming convention removes any possibility of confusion when dealing with menus.

Now, on to the next menu item. Enter “&New” in the Caption box and “mnuFileNew” in the Name box. Before clicking on the Next button, however, click on the right arrow button. You’ll see the New caption—now displayed with an ellipsis in front of it—move over in the outline box. Essentially, we’ve made the New command subsidiary to the File command. In other words, New will appear as an item on the File menu, not as a separate menu item on the menu bar.

Now click on Next. The new menu item (which is currently blank) is inserted at the same level as the item immediately preceding it (in this case, the New command). Enter “&Open” as the caption and “mnuFileOpen” as the name for this menu item. Open the Shortcut Key list and scroll down, selecting Ctrl+O to specify that the Ctrl+O key combination will be the shortcut for the File|Open command. Notice that Ctrl+O is displayed in the menu outline next to the Open caption.

Add the remaining two commands to the File menu:

  &Save, with the name mnuFileSave and the Ctrl+S shortcut key
  Save File &As, with the name mnuFileSaveFileAs and no shortcut key

At this point, your Menu Editor should look like Figure 11.3.

The next item we need to add to the File menu is not a menu command at all, but rather a separator— a horizontal line separating one section of the menu from another. To add a separator, create a menu item with a caption consisting of just a single dash, or hyphen. Because all menu items must be named, we’ll assign the name mnuFileSeparator to this item. After clicking on Next, add the last item on the File menu, using the caption E&xit and the name mnuFileExit.


Figure 11.3  The Menu Editor after partially completing the File menu.

After entering the Exit command and clicking on Next, you are ready to start with the Edit menu. At this point, any entry you make will be subsidiary to the File menu. Because we want Edit to be a top-level menu, you need to click on the left arrow button to move up one level on the outline. Assign the caption &Edit and the name mnuEdit to this menu item, then click on Next. Click on the right arrow to move down one level on the outline, and add the commands shown in Table 11.1 to the Edit menu.


TIP:  Don’t Stray From The Norm

If you have even a little experience using Windows programs, you have probably noticed that the menus for most programs follow certain conventions. For example, if the program deals with files (and most programs do), the file-related commands are on the File menu, which is the first item on the menu bar. Likewise, the first three entries on the File menu are usually New, Open, and Close. Ctrl+O is the standard shortcut key for the Open command.

Do yourself—and your users—a favor: Follow these standards when organizing menus in your Visual Basic programs. Sure, your unique menu design may be just fine from a functional point of view, but it’s guaranteed to confuse your users.


That’s it—the menu is finished. Your Menu Editor will look like the one shown in Figure 11.4. Click on OK to close the Menu Editor and return to the form. You will see the menus on the form during design and when the program runs. You can open the menus and select commands—although nothing happens, because we haven’t written event code for the menu commands yet. We will be creating the event procedures later.

Before we continue creating the project, let’s go over the other Menu Editor commands. Even though you don’t need them for this project, you will someday.

Table 11.1 Edit menu commands.

Caption Name Shortcut Key
&Copy mnuEditCopy Ctrl+C
Cu&t mnuEditCut Ctrl+X
&Paste mnuEditPaste Ctrl+V
- mnuEdit Separator
&Font mnuEditFont Ctrl+F


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.