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.
Comments