Introduction to Windows Forms

Sub-Menus

MenuItem objects in Windows Forms have their own MenuItem collections. This allows you to create a hierarchical structure of menus that have their own cascading child menus within them. The following listing illustrates this process.

  MenuItem filemenu = new MenuItem();
  filemenu.Text = "&File";
  menu.MenuItems.Add(filemenu);

    MenuItem open = new MenuItem();
    open.Text = "&Open";
    filemenu.MenuItems.Add(open);

    MenuItem print= new MenuItem();
    print.Text = "Print...";
    filemenu.MenuItems.Add(print);

      MenuItem temp= new MenuItem();
      temp.Text = "Pre&view";
      print.MenuItems.Add(temp);

      temp= new MenuItem();
      temp.Text = "To &File";
      print.MenuItems.Add(temp);

    MenuItem exit= new MenuItem();
    exit.Text = "E&xit";
    filemenu.MenuItems.Add(exit);

As before, the indentation shows the menu level. Figure 3.1.10 shows the menu in action.

Figure 3.1.10
Submenus in action.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger