0 out of 2 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.

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class MenuItem : Menu

The MenuItem type exposes the following members.

  Name Description
Public method MenuItem() Initializes a MenuItem with a blank caption.
Public method MenuItem(String) Initializes a new instance of the MenuItem class with a specified caption for the menu item.
Public method MenuItem(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 method MenuItem(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 method MenuItem(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 method MenuItem(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
  Name Description
Public property BarBreak 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).
Public property Break 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).
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property Checked Gets or sets a value indicating whether a check mark appears next to the text of the menu item.
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Public property DefaultItem Gets or sets a value indicating whether the menu item is the default menu item.
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Public property Enabled Gets or sets a value indicating whether the menu item is enabled.
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public property Handle Gets a value representing the window handle for the menu. (Inherited from Menu.)
Public property Index Gets or sets a value indicating the position of the menu item in its parent menu.
Public property IsParent Gets a value indicating whether the menu item contains child menu items. (Overrides Menu.IsParent.)
Public property MdiList 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.
Public property MdiListItem Gets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms. (Inherited from Menu.)
Protected property MenuID Gets a value indicating the Windows identifier for this menu item.
Public property MenuItems Gets a value indicating the collection of MenuItem objects associated with the menu. (Inherited from Menu.)
Public property MergeOrder Gets or sets a value indicating the relative position of the menu item when it is merged with another.
Public property MergeType Gets or sets a value indicating the behavior of this menu item when its menu is merged with another.
Public property Mnemonic Gets a value indicating the mnemonic character that is associated with this menu item.
Public property Name Gets or sets the name of the Menu. (Inherited from Menu.)
Public property OwnerDraw Gets or sets a value indicating whether the code that you provide draws the menu item or Windows draws the menu item.
Public property Parent Gets a value indicating the menu that contains this menu item.
Public property RadioCheck Gets or sets a value indicating whether the MenuItem, if checked, displays a radio-button instead of a check mark.
Public property Shortcut Gets or sets a value indicating the shortcut key associated with the menu item.
Public property ShowShortcut 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.
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Public property Tag Gets or sets user-defined data associated with the control. (Inherited from Menu.)
Public property Text Gets or sets a value indicating the caption of the menu item.
Public property Visible Gets or sets a value indicating whether the menu item is visible.
Top
  Name Description
Public method CloneMenu() Creates a copy of the current MenuItem.
Protected method CloneMenu(Menu) Copies the Menu that is passed as a parameter to the current Menu. (Inherited from Menu.)
Protected method CloneMenu(MenuItem) Creates a copy of the specified MenuItem.
Protected method CreateMenuHandle Creates a new handle to the Menu. (Inherited from Menu.)
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Disposes of the resources (other than memory) used by the MenuItem. (Overrides Menu.Dispose(Boolean).)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public method FindMenuItem Gets the MenuItem that contains the value specified. (Inherited from Menu.)
Protected method FindMergePosition Returns the position at which a menu item should be inserted into the menu. (Inherited from Menu.)
Public method GetContextMenu Gets the ContextMenu that contains this menu. (Inherited from Menu.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetMainMenu Gets the MainMenu that contains this menu. (Inherited from Menu.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method MergeMenu() Merges this MenuItem with another MenuItem and returns the resulting merged MenuItem.
Public method MergeMenu(Menu) Merges the MenuItem objects of one menu with the current menu. (Inherited from Menu.)
Public method MergeMenu(MenuItem) Merges another menu item with this menu item.
Protected method OnClick Raises the Click event.
Protected method OnDrawItem Raises the DrawItem event.
Protected method OnInitMenuPopup Infrastructure. Raises the Popup event.
Protected method OnMeasureItem Raises the MeasureItem event.
Protected method OnPopup Raises the Popup event.
Protected method OnSelect Raises the Select event.
Public method PerformClick Generates a Click event for the MenuItem, simulating a click by a user.
Public method PerformSelect Raises the Select event for this menu item.
Protected method ProcessCmdKey Processes a command key. (Inherited from Menu.)
Public method ToString Infrastructure. Returns a string that represents the MenuItem. (Overrides Menu.ToString().)
Top
  Name Description
Public event Click Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public event DrawItem Occurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.
Public event MeasureItem Occurs when the menu needs to know the size of a menu item before drawing it.
Public event Popup Occurs before a menu item's list of menu items is displayed.
Public event Select Occurs 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, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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)
Community Content Add
Annotations FAQ