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

The MenuItem type exposes the following members.

  NameDescription
Public methodMenuItem()Initializes a MenuItem with a blank caption.
Public methodMenuItem(String)Initializes a new instance of the MenuItem class with a specified caption for the menu item.
Public methodMenuItem(String, EventHandler)Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.
Public methodMenuItem(String, MenuItem[])Initializes a new instance of the class with a specified caption and an array of submenu items defined for the menu item.
Public methodMenuItem(String, EventHandler, Shortcut)Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for the menu item.
Public methodMenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[])Initializes a new instance of the MenuItem class with a specified caption; defined event-handlers for the Click, Select and Popup events; a shortcut key; a merge type; and order specified for the menu item.
Top
  NameDescription
Public propertyBarBreakGets or sets a value indicating whether the MenuItem is placed on a new line (for a menu item added to a MainMenu object) or in a new column (for a submenu item or menu item displayed in a ContextMenu).
Public propertyBreakGets or sets a value indicating whether the item is placed on a new line (for a menu item added to a MainMenu object) or in a new column (for a menu item or submenu item displayed in a ContextMenu).
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyCheckedGets or sets a value indicating whether a check mark appears next to the text of the menu item.
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Public propertyDefaultItemGets or sets a value indicating whether the menu item is the default menu item.
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Public propertyEnabledGets or sets a value indicating whether the menu item is enabled.
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyHandleGets a value representing the window handle for the menu. (Inherited from Menu.)
Public propertyIndexGets or sets a value indicating the position of the menu item in its parent menu.
Public propertyIsParentGets a value indicating whether the menu item contains child menu items. (Overrides Menu.IsParent.)
Public propertyMdiListGets or sets a value indicating whether the menu item will be populated with a list of the Multiple Document Interface (MDI) child windows that are displayed within the associated form.
Public propertyMdiListItemGets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms. (Inherited from Menu.)
Protected propertyMenuIDGets a value indicating the Windows identifier for this menu item.
Public propertyMenuItemsGets a value indicating the collection of MenuItem objects associated with the menu. (Inherited from Menu.)
Public propertyMergeOrderGets or sets a value indicating the relative position of the menu item when it is merged with another.
Public propertyMergeTypeGets or sets a value indicating the behavior of this menu item when its menu is merged with another.
Public propertyMnemonicGets a value indicating the mnemonic character that is associated with this menu item.
Public propertyNameGets or sets the name of the Menu. (Inherited from Menu.)
Public propertyOwnerDrawGets or sets a value indicating whether the code that you provide draws the menu item or Windows draws the menu item.
Public propertyParentGets a value indicating the menu that contains this menu item.
Public propertyRadioCheckGets or sets a value indicating whether the MenuItem, if checked, displays a radio-button instead of a check mark.
Public propertyShortcutGets or sets a value indicating the shortcut key associated with the menu item.
Public propertyShowShortcutGets or sets a value indicating whether the shortcut key that is associated with the menu item is displayed next to the menu item caption.
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Public propertyTagGets or sets user-defined data associated with the control. (Inherited from Menu.)
Public propertyTextGets or sets a value indicating the caption of the menu item.
Public propertyVisibleGets or sets a value indicating whether the menu item is visible.
Top
  NameDescription
Public methodCloneMenu()Creates a copy of the current MenuItem.
Protected methodCloneMenu(Menu)Copies the Menu that is passed as a parameter to the current Menu. (Inherited from Menu.)
Protected methodCloneMenu(MenuItem)Creates a copy of the specified MenuItem.
Protected methodCreateMenuHandleCreates a new handle to the Menu. (Inherited from Menu.)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Disposes of the resources (other than memory) used by the MenuItem. (Overrides Menu.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public methodFindMenuItemGets the MenuItem that contains the value specified. (Inherited from Menu.)
Protected methodFindMergePositionReturns the position at which a menu item should be inserted into the menu. (Inherited from Menu.)
Public methodGetContextMenuGets the ContextMenu that contains this menu. (Inherited from Menu.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodGetMainMenuGets the MainMenu that contains this menu. (Inherited from Menu.)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodMergeMenu()Merges this MenuItem with another MenuItem and returns the resulting merged MenuItem.
Public methodMergeMenu(Menu)Merges the MenuItem objects of one menu with the current menu. (Inherited from Menu.)
Public methodMergeMenu(MenuItem)Merges another menu item with this menu item.
Protected methodOnClickRaises the Click event.
Protected methodOnDrawItemRaises the DrawItem event.
Protected methodOnInitMenuPopupInfrastructure. Raises the Popup event.
Protected methodOnMeasureItemRaises the MeasureItem event.
Protected methodOnPopupRaises the Popup event.
Protected methodOnSelectRaises the Select event.
Public methodPerformClickGenerates a Click event for the MenuItem, simulating a click by a user.
Public methodPerformSelectRaises the Select event for this menu item.
Protected methodProcessCmdKeyProcesses a command key. (Inherited from Menu.)
Public methodToStringInfrastructure. Returns a string that represents the MenuItem. (Overrides Menu.ToString().)
Top
  NameDescription
Public eventClickOccurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public eventDrawItemOccurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.
Public eventMeasureItemOccurs when the menu needs to know the size of a menu item before drawing it.
Public eventPopupOccurs before a menu item's list of menu items is displayed.
Public eventSelectOccurs when the user places the pointer over a menu item.
Top

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.