Adding a Menu Shortcut

I l @ ve RuBoard

Placing an ampersand before a character in the menu text will automatically give the menu item an underscore when the Alt key is pressed. The key combination of Alt+F followed by O can be used to invoke the menu handler as if the menu were selected with the mouse.

A direct key combination might also be added to the menu item by using one of the predefined Shortcut enumerations. The File, Open menu item handler can be made to fire in response to a Ctrl+O keypress by adding the shortcut, as shown in Listing 3.1.6.

Listing 3.1.6 Adding a Shortcut to the File, Open MenuItem
 MenuItem filemenu = new MenuItem(); filemenu.Text = "&File"; menu.MenuItems.Add(filemenu);     MenuItem open = new MenuItem();     open.Text = "&Open";     filemenu.MenuItems.Add(open);     open.Shortcut = Shortcut.CtrlO;     open.ShowShortcut = true; 

When you press the Alt key, the F in the File menu is underlined . You can press F to pop up the menu and press O to invoke the menu's function, as shown in Figure 3.1.4.

Figure 3.1.4. Menu shortcuts in action.

graphics/0301fig04.gif

Note how the Open menu is appended with the shortcut key press combination Ctrl+O by the MenuItem.ShowShortcut property setting.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

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