One Step Further: Assigning Shortcut Keys to Menus


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

  1. Make sure that your program has stopped running and is in design mode.

    You can modify a program only when it isn't running.

  2. 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.

  3. 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.

  4. Click the Ctrl check box, click the Key list box, and select the letter T in the list.

    The Properties window looks like this:

    graphic

    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.

  5. 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.

  6. Click the Start Debugging button on the Standard toolbar.

  7. Press Ctrl+D to run the Date command.

    The current date appears in the program.

  8. Press Ctrl+T to run the Time command.

    The current time appears in the program.

  9. 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.

    graphic

  10. 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.”



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net