MenuItem Class
TOC
Collapse the table of content
Expand the table of content

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)

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

public class MenuItem : Menu

NameDescription
System_CAPS_pubmethodMenuItem()

Initializes a MenuItem with a blank caption.

System_CAPS_pubmethodMenuItem(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.

System_CAPS_pubmethodMenuItem(String)

Initializes a new instance of the MenuItem class with a specified caption for the menu item.

System_CAPS_pubmethodMenuItem(String, EventHandler)

Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.

System_CAPS_pubmethodMenuItem(String, EventHandler, Shortcut)

Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for the menu item.

System_CAPS_pubmethodMenuItem(String, MenuItem[])

Initializes a new instance of the class with a specified caption and an array of submenu items defined for the menu item.

NameDescription
System_CAPS_pubpropertyBarBreak

Gets 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).

System_CAPS_pubpropertyBreak

Gets 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).

System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyChecked

Gets or sets a value indicating whether a check mark appears next to the text of the menu item.

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubpropertyDefaultItem

Gets or sets a value indicating whether the menu item is the default menu item.

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_pubpropertyEnabled

Gets or sets a value indicating whether the menu item is enabled.

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyHandle

Gets a value representing the window handle for the menu.(Inherited from Menu.)

System_CAPS_pubpropertyIndex

Gets or sets a value indicating the position of the menu item in its parent menu.

System_CAPS_pubpropertyIsParent

Gets a value indicating whether the menu item contains child menu items.(Overrides Menu.IsParent.)

System_CAPS_pubpropertyMdiList

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

System_CAPS_pubpropertyMdiListItem

Gets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.(Inherited from Menu.)

System_CAPS_protpropertyMenuID

Gets a value indicating the Windows identifier for this menu item.

System_CAPS_pubpropertyMenuItems

Gets a value indicating the collection of MenuItem objects associated with the menu.(Inherited from Menu.)

System_CAPS_pubpropertyMergeOrder

Gets or sets a value indicating the relative position of the menu item when it is merged with another.

System_CAPS_pubpropertyMergeType

Gets or sets a value indicating the behavior of this menu item when its menu is merged with another.

System_CAPS_pubpropertyMnemonic

Gets a value indicating the mnemonic character that is associated with this menu item.

System_CAPS_pubpropertyName

Gets or sets the name of the Menu.(Inherited from Menu.)

System_CAPS_pubpropertyOwnerDraw

Gets or sets a value indicating whether the code that you provide draws the menu item or Windows draws the menu item.

System_CAPS_pubpropertyParent

Gets a value indicating the menu that contains this menu item.

System_CAPS_pubpropertyRadioCheck

Gets or sets a value indicating whether the MenuItem, if checked, displays a radio-button instead of a check mark.

System_CAPS_pubpropertyShortcut

Gets or sets a value indicating the shortcut key associated with the menu item.

System_CAPS_pubpropertyShowShortcut

Gets or sets a value indicating whether the shortcut key that is associated with the menu item is displayed next to the menu item caption.

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_pubpropertyTag

Gets or sets user-defined data associated with the control.(Inherited from Menu.)

System_CAPS_pubpropertyText

Gets or sets a value indicating the caption of the menu item.

System_CAPS_pubpropertyVisible

Gets or sets a value indicating whether the menu item is visible.

NameDescription
System_CAPS_pubmethodCloneMenu()

Creates a copy of the current MenuItem.

System_CAPS_protmethodCloneMenu(Menu)

Copies the Menu that is passed as a parameter to the current Menu.(Inherited from Menu.)

System_CAPS_protmethodCloneMenu(MenuItem)

Creates a copy of the specified MenuItem.

System_CAPS_protmethodCreateMenuHandle()

Creates a new handle to the Menu.(Inherited from Menu.)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Disposes of the resources (other than memory) used by the MenuItem.(Overrides Menu.Dispose(Boolean).)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodFindMenuItem(Int32, IntPtr)

Gets the MenuItem that contains the value specified. (Inherited from Menu.)

System_CAPS_protmethodFindMergePosition(Int32)

Returns the position at which a menu item should be inserted into the menu.(Inherited from Menu.)

System_CAPS_pubmethodGetContextMenu()

Gets the ContextMenu that contains this menu.(Inherited from Menu.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetMainMenu()

Gets the MainMenu that contains this menu.(Inherited from Menu.)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodMergeMenu()

Merges this MenuItem with another MenuItem and returns the resulting merged MenuItem.

System_CAPS_pubmethodMergeMenu(Menu)

Merges the MenuItem objects of one menu with the current menu.(Inherited from Menu.)

System_CAPS_pubmethodMergeMenu(MenuItem)

Merges another menu item with this menu item.

System_CAPS_protmethodOnClick(EventArgs)

Raises the Click event.

System_CAPS_protmethodOnDrawItem(DrawItemEventArgs)

Raises the DrawItem event.

System_CAPS_protmethodOnInitMenuPopup(EventArgs)

This API supports the product infrastructure and is not intended to be used directly from your code. Raises the Popup event.

System_CAPS_protmethodOnMeasureItem(MeasureItemEventArgs)

Raises the MeasureItem event.

System_CAPS_protmethodOnPopup(EventArgs)

Raises the Popup event.

System_CAPS_protmethodOnSelect(EventArgs)

Raises the Select event.

System_CAPS_pubmethodPerformClick()

Generates a Click event for the MenuItem, simulating a click by a user.

System_CAPS_pubmethodPerformSelect()

Raises the Select event for this menu item.

System_CAPS_protmethodProcessCmdKey(Message, Keys)

Processes a command key.(Inherited from Menu.)

System_CAPS_pubmethodToString()

This API supports the product infrastructure and is not intended to be used directly from your code. Returns a string that represents the MenuItem.(Overrides Menu.ToString().)

NameDescription
System_CAPS_pubeventClick

Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.

System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventDrawItem

Occurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.

System_CAPS_pubeventMeasureItem

Occurs when the menu needs to know the size of a menu item before drawing it.

System_CAPS_pubeventPopup

Occurs before a menu item's list of menu items is displayed.

System_CAPS_pubeventSelect

Occurs when the user places the pointer over a menu item.

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
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft