Standard Windows menus are always located at the top of a form, just under
the title bar. VB programmers can create menus by first selecting the form that
will host the menu and then using the VB Menu Editor, as shown in Figure 3.3.
The Menu Editor is available only when a form is being designed. It is located
on the Tools menu in VB6. The first step in creating a menu is to enter the menu
item's caption. The caption can incorporate the ampersand (&) for an access
key designation, also known as an accelerator key. This enables the user to see
an underlined letter in the menu and use the Alt key along with the accelerator
After the caption of the menu item has been set, the menu requires an object
name for programmatic reference. The menu names can be any valid object name.
Naming conventions are preferred to allow for easier reading of source code and
quick identification of objects Menus use the three-letter prefix "mnu" before
the selected name.
FIGURE 3.3 - The Visual Basic Menu Editor.
The remaining task in creating a menu is to decide at which level in the menu
structure this item will appear. Will the object be a toplevel menu item, sub-menu
item, or a sub–sub-menu item? In the list box at the bottom of the Menu
Editor, all top-level menu items are flush with the left side of the list box
border. Sub-level menu items have four small dots preceding the menu caption;
sub– sub-menu items have eight dots. The menu items can also be reorganized
according to position in the menu.
To control the level and position of the menu item being entered, just use
the four direction arrows in the Menu Editor. The up and down arrows reposition
menu items for order. The left and right arrows allow menu items to be top-level,
sub-level or sub–sub-level.
You can implement a separator bar in a menu (a horizontal line that visually
defines the boundary between two groups of menu items) by putting a single dash
(-) as the entire caption for a menu item. This item then becomes the separator
bar on the runtime menu. You must remember that even separator items in a menu
must each have their own unique Name property.
Menu items can also have their appearance properties set at design time through
the Menu Editor. Properties such as Checked, Enabled, Visible, WindowList, and
a shortcut key can all be specified. In Figure 3.3, the Menu Editor check boxes
for the Checked, Enabled, Visible, and WindowList properties are all visible.
In addition a drop-down list of possible shortcut key combinations allows the
programmer to assign a shortcut key to this particular menu item. Unlike the accelerator
or access key mentioned above, the shortcut key can be pressed by the user without
its corresponding menu item being visible. Windows automatically displays the
shortcut key assignment when the menu item is displayed.
All of these properties are also available at runtime except for the shortcut
key. To change the desired property of the menu object, just use the object name
followed by the property name and the value. Examples of such runtime changes
are given in the following section.
Attaching Code to a Menu Item's Click
A menu control has a single event procedure: the Click event procedure. The
Click event procedure is the place where you write or call the code that you want
to execute when the user chooses the menu item.
You can access a menu control's Click event procedure by singleclicking
the menu item from the design time copy of the menu.