Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

MenuItem Class

Represents an individual item that is displayed within a MainMenu or ContextMenu.

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

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

[Visual Basic]
Public Class MenuItem
   Inherits Menu
[C#]
public class MenuItem : Menu
[C++]
public __gc class MenuItem : public Menu
[JScript]
public class MenuItem extends Menu

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

In order for a MenuItem to be displayed, you must add it to a MainMenu or ContextMenu. To create submenus, you can add MenuItem objects to the MenuItems property of the parent MenuItem.

The MenuItem class provides properties that enable you to configure the appearance and functionality of a menu item. To display a check mark next to a menu item, use the Checked property. You can use this feature to identify a menu item that is selected in a list of mutually exclusive menu items. For example, if you have a set of menu items for setting the color of text in a TextBox control, you can use the Checked property to identify which color is currently selected. The Shortcut property can be used to define a keyboard combination that can be pressed to select the menu item.

For MenuItem objects displayed in a Multiple Document Interface (MDI) application, you can use the MergeMenu method to merge the menus of an MDI parent for with that of its child forms to create a consolidated menu structure. Because a MenuItem cannot be reused in multiple locations at the same time, such as in a MainMenu and a ContextMenu, you can use the CloneMenu method to create a copy of a MenuItem for use in another location.

The Popup event enables you to perform tasks before a menu is displayed. For example, you can create an event handler for this event to display or hide menu items based on the state of your code. The Select event enables you to perform tasks such as providing detailed help for your application's menu items when the user places the mouse cursor over a menu item.

Example

[Visual Basic, C#, C++] The following example creates 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

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.