Using Menus


Using Menus

An essential component of the Windows user interface is the use of menus. Menus that are typically located in the top of the application window allow a context-sensitive user interaction mechanism for a broader functionality. Menus are typically hierarchical, and the menu hierarchies provide the user a large number of possible actions in a very compact user interface. Adding a menu to Windows Forms is as simple as setting the Menu property to a MainMenu object (Listing 7.7, Figure 7.11). The MainMenu instance itself can contain a hierarchy of MenuItem instances. MenuItem can have a set of event handlers assigned to provide the required user interaction. Also note that menu items that contain the ampersand (&) character also have an associated keyboard shortcut (Alt+< Character >).

Figure 7.11. Using menus.

As illustrated in the later part of this chapter, menus in Windows Forms can be merged with child menus in Multiple Document Interface (MDI) applications.

Listing 7.7 Using Menus
 using System; using System.Windows.Forms; namespace MyCompany {  namespace MyApp  {    class MenuForm : Form {    public MenuForm()    {       this.Text = "Adding Menus";       MainMenu menu = new MainMenu();       MenuItem fileMenu = new MenuItem("&File");       MenuItem newMenuItem = new MenuItem("&New");       MenuItem openMenuItem = new MenuItem("&Open");       MenuItem exitMenuItem = new MenuItem("E&xit");       fileMenu.MenuItems.Add(newMenuItem);       fileMenu.MenuItems.Add(openMenuItem);       fileMenu.MenuItems.Add(exitMenuItem);       menu.MenuItems.Add(fileMenu);       MenuItem helpMenu = new MenuItem("&Help");       MenuItem helpTopicsMenuItem = new MenuItem("&Help Topics");       MenuItem aboutMenuItem = new MenuItem("&About");       helpMenu.MenuItems.Add(helpTopicsMenuItem);       helpMenu.MenuItems.Add("-");       helpMenu.MenuItems.Add(aboutMenuItem);       menu.MenuItems.Add(helpMenu);       this.Menu = menu;       exitMenuItem.Click += new EventHandler(this.ExitMenuItem_Click);       aboutMenuItem.Click += new EventHandler(this.AboutMenuItem_Click);    }    protected void ExitMenuItem_Click(Object sender, EventArgs e)    {       Application.Exit();    }    protected void AboutMenuItem_Click(Object sender, EventArgs e)    {       MessageBox.Show("Simple App by Hitesh Seth. All Rights Reserved.",          "Adding Menus Application",          MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    }    public static void Main()    {       MenuForm mf = new MenuForm();       Application.Run(mf);    }    }  } } 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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