Library sample chapters
Introduction to Windows Forms
Adding a Menu
A Windows application without a menu is a rare thing. A Windows Forms application is no exception. Like the button and label you saw earlier, the menu component can be added to the Menu member of the main application, and events from the menu items can be hooked to handlers.
Menus under .NET come in two forms. MainMenu is applied to a form to provide the main user interface menu, and ContextMenu is used to respond to right mouse clicks. In both cases, the individual items within the menus are objects of type MenuItem. A menu is constructed as a hierarchy of parent and child objects. The main menu owns the individual drop-downs, which in turn own their menu items.
A typical menu creation sequence is seen in Listing 3.1.5.
Listing 3.1.5 Constructing a Menu
MainMenu menu = new MainMenu();
MenuItem filemenu = new MenuItem();
filemenu.Text = "File";
menu.MenuItems.Add(filemenu);
MenuItem open = new MenuItem();
open.Text = "Open";
filemenu.MenuItems.Add(open);
MenuItem save= new MenuItem();
save.Text = "Save";
filemenu.MenuItems.Add(save);
MenuItem exit= new MenuItem();
exit.Text = "Exit";
filemenu.MenuItems.Add(exit);
MenuItem editmenu = new MenuItem();
editmenu.Text = "Edit";
menu.MenuItems.Add(editmenu);
MenuItem cut= new MenuItem();
cut.Text = "Cut";
editmenu.MenuItems.Add(cut);
MenuItem copy = new MenuItem();
copy.Text = "Copy";
editmenu.MenuItems.Add(copy);
MenuItem paste = new MenuItem();
paste.Text = "Paste";
editmenu.MenuItems.Add(paste);
this.Menu = menu;
The indentation in Listing 3.1.5 illustrates the hierarchy of the menus.
Figure 3.1.3 shows a simple resizable application with a menu added.
Figure 3.1.3
A simple resizable application with a menu.
Adding a Menu Shortcut
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.
Note how the Open menu is appended with the shortcut key press combination Ctrl+O by the MenuItem.ShowShortcut property setting.
Related articles
Related discussion
-
Help me how to dynamic create row column of TableLayoutpanel at run time ??????
by anatha1 (0 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
How to Export Datagridview contents to Excel
by BarbaMariolino (8 replies)
-
Help accessing sound card
by daz4904 (0 replies)
-
How to Write a GPS Application
by stoyac (19 replies)
Actually all you have to do in place an ampersand before the letter you want underlined.
So if you want the "r" in "Print" underlined, enter in the code:
"P&rint"
That's it.
if you want to view the _ all the time for alt shortcuts you have to go to:
display properties->appearance->effects
and uncheck the "Hide underlined charactors for keyboard navigation until I press the ALT key" checkbox.
these instructions are for XP SP2 Pro build 2600
The "" on the short-cut character is visible only when "ALT" is pressed. Is it possible to display the "" programatically???
This thread is for discussions of Introduction to Windows Forms.