Adding a Menu

I l @ ve RuBoard

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.

graphics/0301fig03.gif

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