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

MenuItem Class

Represents an individual item that is displayed within a MainMenu or ContextMenu. Although ToolStripMenuItem replaces and adds functionality to the MenuItem control of previous versions, MenuItem is retained for both backward compatibility and future use if you choose.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public class MenuItem : Menu
public class MenuItem extends Menu
public class MenuItem extends Menu
Not applicable.

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 pointer over a menu item.

The following code 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 requires that there is a MainMenu object named mainMenu1 and four MenuItem objects named menuItem1, menuItem2, menuItem3, and menuItem4.

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);
    }


public void CreateMyMenu()
{
    // Set the caption for the top-level menu item.
    menuItem1.set_Text("Edit");

    // Set the caption for the first submenu.
    menuItem2.set_Text("Font Size");

    // Set the caption for menuItem2's first submenu.
    menuItem3.set_Text("Small");

    // Set the checked property to true since this is the default value.
    menuItem3.set_Checked(true);

    // Define a shortcut key combination for the menu item.
    menuItem3.set_Shortcut(Shortcut.CtrlS);

    // Set the caption of the second sub menu item of menuItem2.
    menuItem4.set_Text("Large");

    // Define a shortcut key combination for the menu item.
    menuItem4.set_Shortcut(Shortcut.CtrlL);

    // Set the index of the menu item so it is placed below
    //the first submenu item.
    menuItem4.set_Index(1);

    // Add menuItem3 and menuItem4 to menuItem2's list of menu items.
    menuItem2.get_MenuItems().Add(menuItem3);
    menuItem2.get_MenuItems().Add(menuItem4);

    // Add menuItem2 to menuItem1's list of menu items.
    menuItem1.get_MenuItems().Add(menuItem2);

    // Add menuItem1 to the MainMenu for displaying.
    mainMenu1.get_MenuItems().Add(menuItem1);
} //CreateMyMenu

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Menu
        System.Windows.Forms.MenuItem
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.