MenuItem 類別

定義

表示 MainMenuContextMenu 中顯示的個別項目。Represents an individual item that is displayed within a MainMenu or ContextMenu. 雖然 ToolStripMenuItem 會取代並且將功能加入至之前版本的 MenuItem 控制項;不過,也可以選擇保留 MenuItem,以提供回溯相容性 (Backward Compatibility) 以及供未來使用。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. 此範例需要有一個名為 mainMenu1MainMenu 物件,以及四個名為 menuItem1menuItem2menuItem3menuItem4MenuItem 物件。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

備註

為了顯示 MenuItem,您必須將它加入 MainMenuContextMenuIn order for a MenuItem to be displayed, you must add it to a MainMenu or ContextMenu. 若要建立子功能表,您可以將 MenuItem 物件新增至父 MenuItemMenuItems 屬性。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.

對於在多重文件介面(MDI)應用程式中顯示的 MenuItem 物件,您可以使用 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 無法同時在多個位置重複使用(例如在 MainMenuContextMenu中),您可以使用 CloneMenu 方法來建立 MenuItem 的複本,以用於另一個位置。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.

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

使用空白標題來初始化 MenuItemInitializes a MenuItem with a blank caption.

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 類別的新執行個體。Initializes a new instance of the MenuItem class with a specified caption for the menu item.

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)

使用指定的標題、事件處理常式,和功能表項目的相關快速鍵,來初始化類別的新執行個體。Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for 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.

欄位

FindHandle

指定 FindMenuItem(Int32, IntPtr) 方法應該搜尋控制代碼。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

(繼承來源 Menu)
FindShortcut

指定 FindMenuItem(Int32, IntPtr) 方法應該搜尋捷徑。Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

(繼承來源 Menu)

屬性

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

取得或設定值,指出項目是放在新的一行 (對加入至 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

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
Checked

取得或設定值,指出功能表項目的文字旁邊要不要顯示核取記號。Gets or sets a value indicating whether a check mark appears next to the text of the menu item.

Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
DefaultItem

取得或設定值,指出功能表項目是不是預設的功能表項目。Gets or sets a value indicating whether the menu item is the default menu item.

DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
Enabled

取得或設定值,指出是否啟用功能表項目。Gets or sets a value indicating whether the menu item is enabled.

Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
Handle

取得值,表示功能表的視窗控制代碼 (Window Handle)。Gets a value representing the window handle for the menu.

(繼承來源 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

取得或設定值,指出功能表項目中會不會填入相關表單內顯示的多重文件介面 (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

取得值,指出用來顯示多重文件介面 (MDI) 子表單清單的 MenuItemGets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.

(繼承來源 Menu)
MenuID

取得值,指出這個功能表項目的 Windows 識別項。Gets a value indicating the Windows identifier for this menu item.

MenuItems

取得值,指出與功能表相關的 MenuItem 物件集合。Gets a value indicating the collection of MenuItem objects associated with the menu.

(繼承來源 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

取得或設定 Menu 的名稱。Gets or sets the name of the Menu.

(繼承來源 Menu)
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

取得值,指出含有這個功能表項目的功能表。Gets a value indicating the menu that contains this menu item.

RadioCheck

取得或設定值,指出如果選取 MenuItem,是否顯示選項按鈕,而不顯示核取記號。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

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)
Tag

取得或設定與控制項相關的使用者定義資料。Gets or sets user-defined data associated with the control.

(繼承來源 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.

方法

CloneMenu()

建立目前的 MenuItem 複本。Creates a copy of the current MenuItem.

CloneMenu(Menu)

將以參數傳遞的 Menu 複製到目前的 MenuCopies the Menu that is passed as a parameter to the current Menu.

(繼承來源 Menu)
CloneMenu(MenuItem)

建立目前 MenuItem 的複本。Creates a copy of the specified MenuItem.

CreateMenuHandle()

建立 Menu 的新控制代碼。Creates a new handle to the Menu.

(繼承來源 Menu)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

處置 (Dispose) MenuItem 所使用的資源 (除了記憶體之外)。Disposes of the resources (other than memory) used by the MenuItem.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
FindMenuItem(Int32, IntPtr)

取得包含指定值的 MenuItemGets the MenuItem that contains the value specified.

(繼承來源 Menu)
FindMergePosition(Int32)

傳回功能表項目應該插入功能表上的位置。Returns the position at which a menu item should be inserted into the menu.

(繼承來源 Menu)
GetContextMenu()

取得含有這個功能表的 ContextMenuGets the ContextMenu that contains this menu.

(繼承來源 Menu)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetMainMenu()

取得含有這個功能表的 MainMenuGets the MainMenu that contains this menu.

(繼承來源 Menu)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
MergeMenu()

這個 MenuItem 和另一 MenuItem 合併後傳回合併產生的 MenuItemMerges this MenuItem with another MenuItem and returns the resulting merged MenuItem.

MergeMenu(Menu)

將一個功能表的 MenuItem 物件和目前的功能表合併。Merges the MenuItem objects of one menu with the current menu.

(繼承來源 Menu)
MergeMenu(MenuItem)

合併這個功能表項目與另一功能表項目。Merges another menu item with this menu item.

OnClick(EventArgs)

引發 Click 事件。Raises the Click event.

OnDrawItem(DrawItemEventArgs)

引發 DrawItem 事件。Raises the DrawItem event.

OnInitMenuPopup(EventArgs)

引發 Popup 事件。Raises the Popup event.

OnMeasureItem(MeasureItemEventArgs)

引發 MeasureItem 事件。Raises the MeasureItem event.

OnPopup(EventArgs)

引發 Popup 事件。Raises the Popup event.

OnSelect(EventArgs)

引發 Select 事件。Raises the Select event.

PerformClick()

產生 ClickMenuItem 事件,模擬使用者按下的動作。Generates a Click event for the MenuItem, simulating a click by a user.

PerformSelect()

引發這個功能表項目的 Select 事件。Raises the Select event for this menu item.

ProcessCmdKey(Message, Keys)

處理命令按鍵。Processes a command key.

(繼承來源 Menu)
ToString()

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

事件

Click

發生於按一下功能表項目,或使用為功能表項目定義的快速鍵或便捷鍵選取時。Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.

Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)
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

發生於功能表繪製前需要先知道功能表項目的大小時。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.

適用於

另請參閱