Επεξεργασία

Κοινή χρήση μέσω


MenuItem Class

Definition

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

This class is not available in .NET Core 3.1 and later versions. Use ToolStripMenuItem instead, which replaces the MenuItem control.

public ref class MenuItem : System::Windows::Forms::Menu
public class MenuItem : System.Windows.Forms.Menu
type MenuItem = class
    inherit Menu
Public Class MenuItem
Inherits Menu
Inheritance

Examples

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

Remarks

This class is not available in .NET Core 3.1 and later versions. Use ToolStripMenuItem instead.

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.

Constructors

MenuItem()

Initializes a MenuItem with a blank caption.

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.

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.

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.

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.

MenuItem(String)

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

Fields

FindHandle

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

(Inherited from Menu)
FindShortcut

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

(Inherited from Menu)

Properties

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

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

CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Checked

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

Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DefaultItem

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

DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Enabled

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

Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Handle

Gets a value representing the window handle for the menu.

(Inherited from Menu)
Index

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

IsParent

Gets a value indicating whether the menu item contains child menu items.

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.

MdiListItem

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

(Inherited from Menu)
MenuID

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

MenuItems

Gets a value indicating the collection of MenuItem objects associated with the menu.

(Inherited from Menu)
MergeOrder

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

MergeType

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

Mnemonic

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

Name

Gets or sets the name of the Menu.

(Inherited from Menu)
OwnerDraw

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

Parent

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

RadioCheck

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

Shortcut

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

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.

Site

Gets or sets the ISite of the Component.

(Inherited from Component)
Tag

Gets or sets user-defined data associated with the control.

(Inherited from Menu)
Text

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

Visible

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

Methods

CloneMenu()

Creates a copy of the current MenuItem.

CloneMenu(Menu)

Copies the Menu that is passed as a parameter to the current Menu.

(Inherited from Menu)
CloneMenu(MenuItem)

Creates a copy of the specified MenuItem.

CreateMenuHandle()

Creates a new handle to the Menu.

(Inherited from Menu)
CreateObjRef(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)
Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

Disposes of the resources (other than memory) used by the MenuItem.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindMenuItem(Int32, IntPtr)

Gets the MenuItem that contains the value specified.

(Inherited from Menu)
FindMergePosition(Int32)

Returns the position at which a menu item should be inserted into the menu.

(Inherited from Menu)
GetContextMenu()

Gets the ContextMenu that contains this menu.

(Inherited from Menu)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetMainMenu()

Gets the MainMenu that contains this menu.

(Inherited from Menu)
GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MergeMenu()

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

MergeMenu(Menu)

Merges the MenuItem objects of one menu with the current menu.

(Inherited from Menu)
MergeMenu(MenuItem)

Merges another menu item with this menu item.

OnClick(EventArgs)

Raises the Click event.

OnDrawItem(DrawItemEventArgs)

Raises the DrawItem event.

OnInitMenuPopup(EventArgs)

Raises the Popup event.

OnMeasureItem(MeasureItemEventArgs)

Raises the MeasureItem event.

OnPopup(EventArgs)

Raises the Popup event.

OnSelect(EventArgs)

Raises the Select event.

PerformClick()

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

PerformSelect()

Raises the Select event for this menu item.

ProcessCmdKey(Message, Keys)

Processes a command key.

(Inherited from Menu)
ToString()

Returns a string that represents the MenuItem.

Events

Click

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

Disposed

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

(Inherited from Component)
DrawItem

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

MeasureItem

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

Popup

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

Select

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

Applies to

See also