Adding Access Keys to Menu Commands


Adding Access Keys to Menu Commands

With most applications, you can access and execute menu commands by using the keyboard. For example, in Visual Studio you can open the File menu by pressing the Alt key and then pressing the F key. Once the File menu is open, you can open a project by pressing the P key. The key that you press in addition to the Alt key and the key that you press to execute a command in an open menu are called access keys. You can identify the access key of a menu item because it's underlined.

Visual Studio makes it easy to provide access key support. To add an access key to a menu item, activate the Menu Designer, and then type an ampersand (&) before the appropriate letter in the menu name. When you open the menu at run time (when the program is running), your program automatically supports the access key.

NOTE
By default, Microsoft Windows 2000, Microsoft Windows XP, and Microsoft Windows Server 2003 don't display the underline for access keys in a program until you press the Alt key for the first time. In Windows 2000, you can turn off this option on the Effects tab of the Display control panel. In Windows XP and Windows Server 2003, you can turn off this option by using the Effects button on the Appearance tab of the Display Properties control panel.

Try adding access keys to the Clock menu now.

Menu Conventions

By convention, each menu title and menu command in a Windows application has an initial capital letter. File and Edit are often the first two menu names on the menu bar, and Help is usually the last. Other common menu names are View, Format, and Window. No matter what menus and commands you use in your applications, take care to be clear and consistent with them. Menus and commands should be easy to use and should have as much in common as possible with those in other Windows–based applications. As you create menu items, use the following guidelines:

  • Use short, specific captions consisting of one or two words at most.

  • Assign each menu item an access key. Use the first letter of the item if possible, or the access key that is commonly assigned (such as x for Exit).

  • Menu items at the same level must have a unique access key.

  • If a command is used as an on/off toggle, place a check mark to the left of the item when it's active. You can add a check mark by setting the Checked property of the menu command to True in the Properties window.

  • Place an ellipsis (…) after a menu command that requires the user to enter more information before the command can be executed. The ellipsis indicates that you'll open a dialog box if the user selects this item.

Add access keys

  1. Click the Clock menu name on the form, pause a moment, and then click it again.

    The menu name is highlighted, and a blinking I-beam (text-editing cursor) appears at the end of the selection. With the I-beam, you can edit your menu name or add the ampersand character (&) for an access key. (If you double-clicked the menu name, the Code Editor might have opened. If that happened, close the Code Editor and repeat step 1.)

  2. Press the Left Arrow key five times to move the I-beam to just before the Clock menu name.

    The I-beam blinks before the letter C in Clock.

  3. Type & to define the letter C as the access key for the Clock menu.

    An ampersand appears in the text box in front of the word Clock.

  4. Click the Date command in the menu list, and then click Date a second time to display the I-beam.

  5. Type & before the letter D.

    The letter D is now defined as the access key for the Date command.

  6. Click the Time command in the menu list, and then click the command a second time to display the I-beam.

  7. Type & before the letter T.

    The letter T is now defined as the access key for the Time command.

  8. Press Enter.

    Pressing Enter locks in your text-editing changes. Your form looks this:

    graphic

Now you'll practice using the Menu Designer to switch the order of the Date and Time commands on the Clock menu. Changing the order of menu items is an important skill because at times you'll think of a better way to define your menus.

Change the order of menu items

  1. Click the Clock menu on the form to display its menu items.

    To change the order of a menu item, simply drag the item to a new location on the menu. Try it now.

  2. Drag the Time menu on top of the Date menu, and then release the mouse button.

    Dragging one menu item on top of another menu item means that you want to place the first menu item ahead of the second menu item on the menu. As quickly as that, Visual Studio moved the Time menu item ahead of the Date item.

You've finished creating the user interface for the Clock menu. Now you'll use the menu event procedures to process the user's menu selections in the program.

NOTE
To delete a menu item from a menu, click the unwanted item in the menu list, and then press the Delete key. (If you try this now, remember that Visual Studio also has an Undo command, located on both the Edit menu and the Standard toolbar, so you can reverse the effects of the deletion.)