MenuItem MenuItem MenuItem MenuItem Class

定义

表示在 MainMenuContextMenu 内显示的单个项。Represents an individual item that is displayed within a MainMenu or ContextMenu. 尽管 ToolStripMenuItem 替换了早期版本的 MenuItem 控件并添加了功能;但是也可选择保留 MenuItem 以备向后兼容和将来使用。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.

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
继承

示例

下面的代码示例创建窗体的菜单结构。The following code example creates a menu structure for a form. 示例代码添加一个MenuItem来表示顶级菜单项,将一个子菜单项添加到其中,以选择字体大小,然后将两个子菜单项添加到表示应用程序中的大型和小字体选择的菜单项。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. 该示例MainMenu要求有一个名mainMenu1为的对象和MenuItem menuItem2 menuItem1 menuItem4四个名为、 、和的对象。menuItem3The 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

注解

为了显示,您必须将其添加MainMenu到或ContextMenuMenuItemIn order for a MenuItem to be displayed, you must add it to a MainMenu or ContextMenu. 若要创建子菜单,可以MenuItem将对象添加MenuItems到父级MenuItem的属性。To create submenus, you can add MenuItem objects to the MenuItems property of the parent MenuItem.

MenuItem类提供使您能够配置菜单项的外观和功能的属性。The MenuItem class provides properties that enable you to configure the appearance and functionality of a menu item. 若要在菜单项旁显示复选标记,请使用Checked属性。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. 例如,如果您有一组菜单项用于设置TextBox控件中文本的颜色,则可以Checked使用属性来识别当前选定的颜色。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. Shortcut属性可用于定义可以按下以选择菜单项的键盘组合。The Shortcut property can be used to define a keyboard combination that can be pressed to select the menu item.

对于MenuItem在多文档界面(MDI)应用程序中显示的对象,可以MergeMenu使用方法来合并 MDI 父级的菜单及其子窗体的菜单,以创建合并的菜单结构。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. MenuItem CloneMenu ContextMenu MainMenu由于不能同时在多个位置重复使用(如在和中),因此可以使用方法创建的副本,以便在其他位置使用。 MenuItemBecause 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.

Popup事件使您能够在显示菜单之前执行任务。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. Select用户将鼠标指针放在菜单项上时,可以使用事件来执行任务,例如,提供应用程序菜单项的详细帮助。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.

构造函数

MenuItem() MenuItem() MenuItem() MenuItem()

使用空白标题初始化 MenuItemInitializes a MenuItem with a blank caption.

MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[]) MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[]) MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[]) MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[])

用指定标题、为(MenuItemClickSelect)事件定义的事件处理程序、快捷键、合并类型和为菜单项指定的顺序来初始化 Popup 类的一个新实例。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) MenuItem(String) MenuItem(String) MenuItem(String)

使用菜单项的指定标题初始化 MenuItem 类的一个新实例。Initializes a new instance of the MenuItem class with a specified caption for the menu item.

MenuItem(String, EventHandler) MenuItem(String, EventHandler) MenuItem(String, EventHandler) MenuItem(String, EventHandler)

用指定标题和菜单项的 Click 事件的事件处理程序初始化该类的一个新实例。Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.

MenuItem(String, EventHandler, Shortcut) MenuItem(String, EventHandler, Shortcut) MenuItem(String, EventHandler, Shortcut) 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, MenuItem[]) MenuItem(String, MenuItem[]) MenuItem(String, MenuItem[]) 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.

属性

BarBreak BarBreak BarBreak BarBreak

获取或设置一个值,通过该值指示是将 MenuItem 放在新行上(对于添加到 MainMenu 对象的菜单项),还是放在新列中(对于 ContextMenu 中显示的子菜单项或菜单项)。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 Break Break Break

获取或设置一个值,通过该值指示是将该项放在新行上(对于添加到 MainMenu 对象的菜单项),还是放在新列中(对于 ContextMenu 中显示的菜单项或子菜单项)。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 CanRaiseEvents CanRaiseEvents CanRaiseEvents

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Checked Checked Checked Checked

获取或设置一个值,通过该值指示选中标记是否出现在菜单项文本的旁边。Gets or sets a value indicating whether a check mark appears next to the text of the menu item.

Container Container Container Container

获取 IContainer,它包含 ComponentGets the IContainer that contains the Component.

(Inherited from Component)
DefaultItem DefaultItem DefaultItem DefaultItem

获取或设置一个值,通过该值指示菜单项是否为默认菜单项。Gets or sets a value indicating whether the menu item is the default menu item.

DesignMode DesignMode DesignMode DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Enabled Enabled Enabled Enabled

获取或设置一个值,通过该值指示菜单项是否启用。Gets or sets a value indicating whether the menu item is enabled.

Events Events Events Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Handle Handle Handle Handle

获取表示菜单的窗口句柄的值。Gets a value representing the window handle for the menu.

(Inherited from Menu)
Index Index Index Index

获取或设置一个值,通过该值指示菜单项在其父菜单中的位置。Gets or sets a value indicating the position of the menu item in its parent menu.

IsParent IsParent IsParent IsParent

获取一个值,通过该值指示菜单项是否包含子菜单项。Gets a value indicating whether the menu item contains child menu items.

MdiList MdiList MdiList MdiList

获取或设置一个值,通过该值指示是否使用在关联窗体内显示的多文档界面 (MDI) 子窗口列表来填充菜单项。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 MdiListItem MdiListItem MdiListItem

获取一个值,通过该值指示用于显示多文档界面 (MDI) 子窗体列表的 MenuItemGets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.

(Inherited from Menu)
MenuID MenuID MenuID MenuID

获取一个值,通过该值指示此菜单项的 Windows 标识符。Gets a value indicating the Windows identifier for this menu item.

MenuItems MenuItems MenuItems MenuItems

获取一个值,通过该值指示与菜单关联的 MenuItem 对象的集合。Gets a value indicating the collection of MenuItem objects associated with the menu.

(Inherited from Menu)
MergeOrder MergeOrder MergeOrder MergeOrder

获取或设置一个值,通过该值指示菜单项与另一个项合并时的相对位置。Gets or sets a value indicating the relative position of the menu item when it is merged with another.

MergeType MergeType MergeType MergeType

获取或设置一个值,通过该值指示该菜单项的菜单与另一个菜单合并时该菜单项的行为。Gets or sets a value indicating the behavior of this menu item when its menu is merged with another.

Mnemonic Mnemonic Mnemonic Mnemonic

获取一个值,通过该值指示与此菜单项关联的助记字符。Gets a value indicating the mnemonic character that is associated with this menu item.

Name Name Name Name

获取或设置 Menu 的名称。Gets or sets the name of the Menu.

(Inherited from Menu)
OwnerDraw OwnerDraw OwnerDraw OwnerDraw

获取或设置一个字值,通过该值指示是由所提供的代码绘制菜单项还是由 Windows 绘制菜单项。Gets or sets a value indicating whether the code that you provide draws the menu item or Windows draws the menu item.

Parent Parent Parent Parent

获取一个值,该值指示包含此菜单项的菜单。Gets a value indicating the menu that contains this menu item.

RadioCheck RadioCheck RadioCheck RadioCheck

获取或设置一个值,通过该值指示 MenuItem(如果已选中)是否显示单选按钮而不是选中标记。Gets or sets a value indicating whether the MenuItem, if checked, displays a radio-button instead of a check mark.

Shortcut Shortcut Shortcut Shortcut

获取或设置一个值,通过该值指示与菜单项关联的快捷键。Gets or sets a value indicating the shortcut key associated with the menu item.

ShowShortcut ShowShortcut ShowShortcut 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 Site Site Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(Inherited from Component)
Tag Tag Tag Tag

获取或设置与控件关联的用户定义的数据。Gets or sets user-defined data associated with the control.

(Inherited from Menu)
Text Text Text Text

获取或设置一个值,通过该值指示菜单项标题。Gets or sets a value indicating the caption of the menu item.

Visible Visible Visible Visible

获取或设置一个值,通过该值指示菜单项是否可见。Gets or sets a value indicating whether the menu item is visible.

方法

CloneMenu() CloneMenu() CloneMenu() CloneMenu()

创建当前 MenuItem 的副本。Creates a copy of the current MenuItem.

CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu)

复制作为参数被传递到当前 MenuMenuCopies the Menu that is passed as a parameter to the current Menu.

(Inherited from Menu)
CloneMenu(MenuItem) CloneMenu(MenuItem) CloneMenu(MenuItem) CloneMenu(MenuItem)

创建指定 MenuItem 的副本。Creates a copy of the specified MenuItem.

CreateMenuHandle() CreateMenuHandle() CreateMenuHandle() CreateMenuHandle()

创建 Menu 的新句柄。Creates a new handle to the Menu.

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

释放由 Component 使用的所有资源。Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

处置由 MenuItem 占用的资源(内存除外)。Disposes of the resources (other than memory) used by the MenuItem.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr)

获取 MenuItem,它包含指定的值。Gets the MenuItem that contains the value specified.

(Inherited from Menu)
FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32)

返回菜单项应插入菜单的位置。Returns the position at which a menu item should be inserted into the menu.

(Inherited from Menu)
GetContextMenu() GetContextMenu() GetContextMenu() GetContextMenu()

获取包含该菜单的 ContextMenuGets the ContextMenu that contains this menu.

(Inherited from Menu)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetMainMenu() GetMainMenu() GetMainMenu() GetMainMenu()

获取包含该菜单的 MainMenuGets the MainMenu that contains this menu.

(Inherited from Menu)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MergeMenu() MergeMenu() MergeMenu() MergeMenu()

将此 MenuItem 与另一个 MenuItem 合并并返回合并后的 MenuItemMerges this MenuItem with another MenuItem and returns the resulting merged MenuItem.

MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu)

将一个菜单的 MenuItem 对象与当前菜单合并。Merges the MenuItem objects of one menu with the current menu.

(Inherited from Menu)
MergeMenu(MenuItem) MergeMenu(MenuItem) MergeMenu(MenuItem) MergeMenu(MenuItem)

将另一菜单项与该菜单项合并。Merges another menu item with this menu item.

OnClick(EventArgs) OnClick(EventArgs) OnClick(EventArgs) OnClick(EventArgs)

引发 Click 事件。Raises the Click event.

OnDrawItem(DrawItemEventArgs) OnDrawItem(DrawItemEventArgs) OnDrawItem(DrawItemEventArgs) OnDrawItem(DrawItemEventArgs)

引发 DrawItem 事件。Raises the DrawItem event.

OnInitMenuPopup(EventArgs) OnInitMenuPopup(EventArgs) OnInitMenuPopup(EventArgs) OnInitMenuPopup(EventArgs)

引发 Popup 事件。Raises the Popup event.

OnMeasureItem(MeasureItemEventArgs) OnMeasureItem(MeasureItemEventArgs) OnMeasureItem(MeasureItemEventArgs) OnMeasureItem(MeasureItemEventArgs)

引发 MeasureItem 事件。Raises the MeasureItem event.

OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs)

引发 Popup 事件。Raises the Popup event.

OnSelect(EventArgs) OnSelect(EventArgs) OnSelect(EventArgs) OnSelect(EventArgs)

引发 Select 事件。Raises the Select event.

PerformClick() PerformClick() PerformClick() PerformClick()

Click 生成一个 MenuItem 事件,模拟用户的单击操作。Generates a Click event for the MenuItem, simulating a click by a user.

PerformSelect() PerformSelect() PerformSelect() PerformSelect()

引发该菜单项的 Select 事件。Raises the Select event for this menu item.

ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys)

处理命令键。Processes a command key.

(Inherited from Menu)
ToString() ToString() ToString() ToString()

返回表示 MenuItem 的字符串。Returns a string that represents the MenuItem.

事件

Click Click Click Click

当单击菜单项或使用为该菜单项定义的快捷键或访问键选择菜单项时发生。Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.

Disposed Disposed Disposed Disposed

当通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)
DrawItem DrawItem DrawItem DrawItem

当菜单项的 OwnerDraw 属性设置为 true 并且发出绘制菜单项的请求时发生。Occurs when the OwnerDraw property of a menu item is set to true and a request is made to draw the menu item.

MeasureItem MeasureItem MeasureItem MeasureItem

当菜单在绘制菜单项之前需要知道菜单项大小时发生。Occurs when the menu needs to know the size of a menu item before drawing it.

Popup Popup Popup Popup

在显示菜单项的菜单项列表之前发生。Occurs before a menu item's list of menu items is displayed.

Select Select Select Select

当用户将指针放在菜单项上时发生。Occurs when the user places the pointer over a menu item.

字段

FindHandle FindHandle FindHandle FindHandle

指定 FindMenuItem(Int32, IntPtr) 方法应搜索一个句柄。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

(Inherited from Menu)
FindShortcut FindShortcut FindShortcut FindShortcut

指定 FindMenuItem(Int32, IntPtr) 方法应搜索一个快捷方式。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

(Inherited from Menu)

适用于

另请参阅