Visual Basic Concepts

Creating Menus with the Menu Editor

See Also

You can use the Menu Editor to create new menus and menu bars, add new commands to existing menus, replace existing menu commands with your own commands, and change and delete existing menus and menu bars.

To display the Menu Editor

This opens the Menu Editor, shown in Figure 6.7.

Figure 6.7   The Menu Editor

While most menu control properties can be set using the Menu Editor, all menu properties are available in the Properties window. The two most important properties for menu controls are:

Other properties in the Menu Editor, including Index, Checked, and NegotiatePosition, are described later in this chapter.

Using the List Box in the Menu Editor

The menu control list box (the lower portion of the Menu Editor) lists all the menu controls for the current form. When you type a menu item in the Caption text box, that item also appears in the menu control list box. Selecting an existing menu control from the list box allows you to edit the properties for that control.

For example, Figure 6.7 shows the menu controls for a File menu in a typical application. The position of the menu control in the menu control list box determines whether the control is a menu title, menu item, submenu title, or submenu item:

Note   A menu control cannot be a separator bar if it is a menu title, has submenu items, is checked or disabled, or has a shortcut key.

To create menu controls in the Menu Editor

  1. Select the form.

  2. From the Tools menu, choose Menu Editor.

    –or–

    Click the Menu Editor button on the toolbar.

  3. In the Caption text box, type the text for the first menu title that you want to appear on the menu bar. Also, place an ampersand (&) before the letter you want to be the access key for that menu item. This letter will automatically be underlined in the menu.

    The menu title text is displayed in the menu control list box.

  4. In the Name text box, type the name that you will use to refer to the menu control in code. See "Menu Title and Naming Guidelines" later in this chapter.

  5. Click the left arrow or right arrow buttons to change the indentation level of the control.

  6. Set other properties for the control, if you choose. You can do this in the Menu Editor or later, in the Properties window.

  7. Choose Next to create another menu control.

    –or–

    Click Insert to add a menu control between existing controls.

    You can also click the up arrow and down arrow buttons to move the control among the existing menu controls.

  8. Choose OK to close the Menu Editor when you have created all the menu controls for that form.

    The menu titles you create are displayed on the form. At design time, click a menu title to drop down its corresponding menu items.

Separating Menu Items

A separator bar is displayed as a horizontal line between items on a menu. On a menu with many items, you can use a separator bar to divide items into logical groups. For example, the File menu in Visual Basic uses separator bars to divide its menu items into three groups, as shown in Figure 6.8.

Figure 6.8   Separator bars

To create a separator bar in the Menu Editor

  1. If you are adding a separator bar to an existing menu, choose Insert to insert a menu control between the menu items you want to separate.

  2. If necessary, click the right arrow button to indent the new menu item to the same level as the menu items it will separate.

  3. Type a hyphen (-) in the Caption text box.

  4. Set the Name property.

  5. Choose OK to close the Menu Editor.

Note   Although separator bars are created as menu controls, they do not respond to the Click event, and users cannot choose them.

Assigning Access Keys and Shortcut Keys

You can improve keyboard access to menu commands by defining access keys and shortcut keys.

Access Keys

Access keys allow the user to open a menu by pressing the ALT key and typing a designated letter. Once a menu is open, the user can choose a control by pressing the letter (the access key) assigned to it. For example, ALT+E might open the Edit menu, and P might select the Paste menu item. An access-key assignment appears as an underlined letter in the menu control's caption, as shown in Figure 6.9.

Figure 6.9   Access keys

To assign an access key to a menu control in the Menu Editor

  1. Select the menu item to which you want to assign an access key.

  2. In the Caption box, type an ampersand (&) immediately in front of the letter you want to be the access key.

For example, if the Edit menu shown in Figure 6.9 is open, the following Caption property settings respond to the corresponding keys.

Menu control caption Caption property Access keys
Cut Cu&t t
Copy C&opy o
Paste

Delete

Select All

Time/Date

&Paste

De&lete

Select &All

Time/&Date

p

l

a

d


Note   Do not use duplicate access keys on menus. If you use the same access key for more than one menu item, the key will not work. For example, if C is the access key for both Cut and Copy, when you select the Edit menu and press C, the Copy command will be selected, but the application will not carry out the command until the user presses ENTER. The Cut command will not be selected at all.

Shortcut Keys

Shortcut keys run a menu item immediately when pressed. Frequently used menu items may be assigned a keyboard shortcut, which provides a single-step method of keyboard access, rather than a three-step method of pressing ALT, a menu title access character, and then a menu item access character. Shortcut key assignments include function key and control key combinations, such as CTRL+F1 or CTRL+A. They appear on the menu to the right of the corresponding menu item, as shown in Figure 6.10.

Figure 6.10   Shortcut keys

To assign a shortcut key to a menu item

  1. Open the Menu Editor.

  2. Select the menu item.

  3. Select a function key or key combination in the Shortcut combo box.

    To remove a shortcut key assignment, choose "(none)" from the top of the list.

Note   Shortcut keys appear automatically on the menu; therefore, you do not have to enter CTRL+key in the Caption box of the Menu Editor.