Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
One Step Further: Assigning Shortcut Keys to Menus
The MenuStrip control lets you assign shortcut keys to your menus. Shortcut keys are key combinations that a user can press to activate a command without using the menu bar. For example, on a typical Edit menu in a Windows application, such as Microsoft Word, you can copy selected text to the Clipboard by pressing Ctrl+C. With the MenuStrip control's ShortcutKeys property, you can customize this setting. Try assigning two shortcut keys to the Clock menu in the Menu program now.
Assign shortcut keys to the Clock menu
Make sure that your program has stopped running and is in design mode.
You can modify a program only when it isn't running.
Click the Clock menu, and then click the Time command to highlight it.
Before you set the shortcut key for a menu command, you must select it. You assign a shortcut key by setting the ShortcutKeys property for the command by using the Properties window. (In Visual Basic .NET 2002 and 2003, this property was named Shortcut.) The menu strip object provides an easy way for you to do this.
Open the Properties window, click the ShortcutKeys property, and then click the arrow in the second column
A pop-up menu appears that helps you assign the shortcut key.
Click the Ctrl check box, click the Key list box, and select the letter T in the list.
The Properties window looks like this:
TIP
Visual Basic normally displays the shortcut key combination in the menu when you run the program, to give users a hint about which keys to press. To hide shortcut key combinations from the user (if you're running out of space), set the ShowShortcutKeys property to False. The shortcut key still works, but users won't see a visual reminder for it. You can also set what will be displayed within the program as a shortcut key by setting the ShortcutKeyDisplayString property.
Click the Date command, and then change its ShortcutKeys property setting to Ctrl+D.
Now you'll run the program and try the shortcut keys.
Click the Start Debugging button on the Standard toolbar.
Press Ctrl+D to run the Date command.
The current date appears in the program.
Press Ctrl+T to run the Time command.
The current time appears in the program.
Click the Clock menu.
The shortcut keys are listed beside the Time and Date commands, as shown in the following illustration. Visual Basic adds these key combinations when you define the shortcuts by using the ShortcutKeys property.
Close the program.
The Menu program ends, and the development environment appears.
You're ready to move deeper into writing programs now, in the part of the book I call “Programming Fundamentals.”