Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Menu Class

Represents the base functionality for all menus.

For a list of all members of this type, see Menu Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Menu
            System.Windows.Forms.ContextMenu
            System.Windows.Forms.MainMenu
            System.Windows.Forms.MenuItem

[Visual Basic]
MustInherit Public Class Menu
   Inherits Component
[C#]
public abstract class Menu : Component
[C++]
public __gc __abstract class Menu : public Component
[JScript]
public abstract class Menu extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

This class is the base class for the MainMenu, MenuItem, and ContextMenu classes. You cannot create an instance of this class. The menus for an application consist of MenuItem objects. These can contain other MenuItem objects, representing submenu items. The MenuItem objects can be stored in a MainMenu for display as an entire menu structure for a form or a ContextMenu that is used to display shortcut menus. This class provides functionality that is common for all the menu classes.

Unlike many base classes, the Menu class uses its derived classes to define many of its properties. If you are using your menu in a Multiple Document Interface (MDI) application, you can use the MdiListItem property to specify a MenuItem that displays a list of open MDI child forms in your application. The MenuItems property contains a list of MenuItem objects stored in the menu class. For a MainMenu or ContextMenu, this property contains all the MenuItem objects that are displayed. For a MenuItem, the MenuItems property represents the submenu items associated with it.

In addition to the properties that are provided for all the derived menu classes, the Menu class also provides methods, such as CloneMenu and MergeMenu, that enable you to create new menus from existing menus, and also merge two menu structures together.

The Menu class also defines the nested class Menu.MenuItemCollection. This class defines the collection of MenuItem objects used by the MenuItems property. You can use the methods of the Menu.MenuItemCollection class to add and remove menu items from a MainMenu, ContextMenu, or MenuItem.

Example

[Visual Basic, C#, C++] The following example uses the derived class, MenuItem, to create a menu structure for a form. The example code adds a MenuItem to represent the top-level menu item, adds a submenu item to it for selecting a font size, and then adds two submenu items to that menu item that represent large and small font choices in an application. The example assumes that there is a MainMenu object named mainMenu1 and four MenuItem objects named menuItem1, menuItem2, menuItem3, and menuItem4.

[Visual Basic] 
Public Sub CreateMyMenu()
    ' Set the caption for the top-level menu item.
    menuItem1.Text = "Edit"
    ' Set the caption for the first submenu.
    menuItem2.Text = "Font Size"
    ' Set the caption for menuItem2's first submenu.
    menuItem3.Text = "Small"
    ' Set the checked property to true since this is the default value.
    menuItem3.Checked = True
    ' Define a shortcut key combination for the menu item.
    menuItem3.Shortcut = Shortcut.CtrlS
    ' Set the caption of the second sub menu item of menuItem2.
    menuItem4.Text = "Large"
    ' Define a shortcut key combination for the menu item.
    menuItem4.Shortcut = Shortcut.CtrlL
    ' Set the index of the menu item so it is placed below the first submenu item.
    menuItem4.Index = 1
    ' Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.MenuItems.Add(menuItem3)
    menuItem2.MenuItems.Add(menuItem4)
    ' Add menuItem2 to menuItem1's list of menu items.
    menuItem1.MenuItems.Add(menuItem2)
    ' Add menuItem1 to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1)
End Sub


[C#] 
public void CreateMyMenu()
    {
    // Set the caption for the top-level menu item.
    menuItem1.Text = "Edit";
    // Set the caption for the first submenu.
    menuItem2.Text = "Font Size";
    // Set the caption for menuItem2's first submenu.
    menuItem3.Text = "Small";
    // Set the checked property to true since this is the default value.
    menuItem3.Checked = true;
    // Define a shortcut key combination for the menu item.
    menuItem3.Shortcut = Shortcut.CtrlS;
    // Set the caption of the second sub menu item of menuItem2.
    menuItem4.Text = "Large";
    // Define a shortcut key combination for the menu item.
    menuItem4.Shortcut = Shortcut.CtrlL;
    // Set the index of the menu item so it is placed below the first submenu item.
    menuItem4.Index = 1;
    // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.MenuItems.Add(menuItem3);
    menuItem2.MenuItems.Add(menuItem4);
    // Add menuItem2 to menuItem1's list of menu items.
    menuItem1.MenuItems.Add(menuItem2);
    // Add menuItem1 to the MainMenu for displaying.
    mainMenu1.MenuItems.Add(menuItem1);
    }


[C++] 
public:
void CreateMyMenu()
    {
    // Set the caption for the top-level menu item.
    menuItem1->Text = S"Edit";
    // Set the caption for the first submenu.
    menuItem2->Text = S"Font Size";
    // Set the caption for menuItem2's first submenu.
    menuItem3->Text = S"Small";
    // Set the checked property to true since this is the default value.
    menuItem3->Checked = true;
    // Define a shortcut key combination for the menu item.
    menuItem3->Shortcut = Shortcut::CtrlS;
    // Set the caption of the second sub menu item of menuItem2.
    menuItem4->Text = S"Large";
    // Define a shortcut key combination for the menu item.
    menuItem4->Shortcut = Shortcut::CtrlL;
    // Set the index of the menu item so it is placed below the first submenu item.
    menuItem4->Index = 1;
    // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2->MenuItems->Add(menuItem3);
    menuItem2->MenuItems->Add(menuItem4);
    // Add menuItem2 to menuItem1's list of menu items.
    menuItem1->MenuItems->Add(menuItem2);
    // Add menuItem1 to the MainMenu for displaying.
    mainMenu1->MenuItems->Add(menuItem1);
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

Menu Members | System.Windows.Forms Namespace | MainMenu | ContextMenu | MenuItem

Show:
© 2014 Microsoft. All rights reserved.