MenuItem 建構函式

定義

初始化 MenuItem 類別的新執行個體。

多載

MenuItem()

使用空白標題來初始化 MenuItem

MenuItem(String)

使用功能表項目的指定標題來初始化 MenuItem 類別的新執行個體。

MenuItem(String, EventHandler)

使用指定的標題和功能表項目的 Click 事件的事件處理常式,初始化類別的新執行個體。

MenuItem(String, MenuItem[])

使用指定標題和為功能表項目定義的子功能表項目陣列,來初始化類別的新執行個體。

MenuItem(String, EventHandler, Shortcut)

使用指定的標題、事件處理常式,和功能表項目的相關快速鍵,來初始化類別的新執行個體。

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

使用指定的標題;為 MenuItemClickSelect 事件定義的事件處理常式;快速鍵;合併型別;和為功能表項目指定的順序,來初始化 Popup 類別的新執行個體。

MenuItem()

使用空白標題來初始化 MenuItem

public:
 MenuItem();
public MenuItem ();
Public Sub New ()

範例

下列程式代碼範例會 MenuItem 使用這個版本的建構函式來建立 。

public:
   void CreateMyMenu()
   {
      // Create an empty menu item object.
      MenuItem^ menuItem1 = gcnew MenuItem;
      // Intialize the menu item using the parameterless version of the constructor.
      // Set the caption of the menu item.
      menuItem1->Text = "&File";
   }
public void CreateMyMenu()
{
   // Create an empty menu item object.
   MenuItem menuItem1 = new MenuItem();
   // Intialize the menu item using the parameterless version of the constructor.
   // Set the caption of the menu item.
   menuItem1.Text = "&File";
}
Public Sub CreateMyMenu()
    ' Create an empty menu item object.
    Dim menuItem1 As New MenuItem()
    ' Intialize the menu item using the parameterless version of the constructor.
    ' Set the caption of the menu item.
    menuItem1.Text = "&File"
End Sub

備註

使用這個建構函式建立空白 MenuItem 之後,您可以使用 類別的屬性 MenuItem 和方法來指定 的外觀 MenuItem和行為。

適用於

MenuItem(String)

使用功能表項目的指定標題來初始化 MenuItem 類別的新執行個體。

public:
 MenuItem(System::String ^ text);
public MenuItem (string text);
new System.Windows.Forms.MenuItem : string -> System.Windows.Forms.MenuItem
Public Sub New (text As String)

參數

text
String

功能表項目的標題。

範例

下列程式代碼範例會建立 ,MenuItem指定建構功能表項時 標題。

public:
   void CreateMyMenus()
   {
      // Create an instance of a MenuItem with a specified caption.
      menuItem1 = gcnew MenuItem( "&File" );
   }
public void CreateMyMenus()
{
   // Create an instance of a MenuItem with a specified caption.
   menuItem1 = new MenuItem("&File");
}
Public Sub CreateMyMenus()
    ' Create an instance of a MenuItem with a specified caption.
    menuItem1 = New MenuItem("&File")
End Sub

備註

當您使用 text 參數指定功能表項的 標題 時,您也可以將 '&' 字元放在做為存取金鑰的字元之前,指定存取鍵。 例如,若要將 “File” 中的 “F” 指定為訪問鍵,您會將功能表項的 標題 指定為 “&File”。 您可以使用此功能來提供選單的鍵盤流覽。

text將 參數設定為 「-」 會導致功能表項顯示為分隔符, (水平線) 而不是標準功能表項。

適用於

MenuItem(String, EventHandler)

使用指定的標題和功能表項目的 Click 事件的事件處理常式,初始化類別的新執行個體。

public:
 MenuItem(System::String ^ text, EventHandler ^ onClick);
public MenuItem (string text, EventHandler onClick);
new System.Windows.Forms.MenuItem : string * EventHandler -> System.Windows.Forms.MenuItem
Public Sub New (text As String, onClick As EventHandler)

參數

text
String

功能表項目的標題。

onClick
EventHandler

EventHandler,處理這個功能表項目的 Click 事件。

範例

下列程式代碼範例會MenuItem建立具有指定之 標題 的物件,以及EventHandler連接到將處理Click功能表項事件之事件處理程式的委派。

public:
   void CreateMyMenuItem()
   {
      // Create an instance of MenuItem with caption and an event handler
      MenuItem^ menuItem1 = gcnew MenuItem( "&New",gcnew System::EventHandler(
         this, &Form1::MenuItem1_Click ) );
   }

private:
   // This method is an event handler for menuItem1 to use when connecting its event handler.
   void MenuItem1_Click( Object^ sender, System::EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Create an instance of MenuItem with caption and an event handler
   MenuItem menuItem1 = new MenuItem("&New", new System.EventHandler(this.MenuItem1_Click));
}

// This method is an event handler for menuItem1 to use when connecting its event handler.
private void MenuItem1_Click(Object sender, System.EventArgs e) 
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
    ' Create an instance of MenuItem with caption and an event 
    ' handler
    Dim MenuItem1 As New MenuItem("&New", New _
        System.EventHandler(AddressOf Me.MenuItem1_Click))
End Sub
' This method is an event handler for MenuItem1 to use when 
' connecting its event handler.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal _
    e as System.EventArgs)
    ' Code goes here that handles the Click event.
End Sub

備註

當您使用 text 參數指定功能表項的 標題 時,您也可以將 『&』 放在做為存取密鑰的字元之前,指定存取鍵。 例如,若要將 “File” 中的 “F” 指定為訪問鍵,您會將功能表項的 標題 指定為 “&File”。 您可以使用此功能來提供選單的鍵盤流覽。

text將 參數設定為 「-」 會導致功能表項顯示為分隔符, (水平線) 而不是標準功能表項。

此外,您可以使用這個建構函式來指定委派,以處理 Click 所建立功能表項的事件。 EventHandler您傳遞至這個建構函式的 必須設定為呼叫可處理Click事件的事件處理程式。 如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

MenuItem(String, MenuItem[])

使用指定標題和為功能表項目定義的子功能表項目陣列,來初始化類別的新執行個體。

public:
 MenuItem(System::String ^ text, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public MenuItem (string text, System.Windows.Forms.MenuItem[] items);
new System.Windows.Forms.MenuItem : string * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Sub New (text As String, items As MenuItem())

參數

text
String

功能表項目的標題。

items
MenuItem[]

MenuItem 物件陣列,含有這個功能表項目的子功能表項目。

範例

下列程式代碼範例會建立具有指定之 標題 的對象,這個事件處理程式會連線至方法,以處理子功能表項目陣列中的每個功能表項的事件。

public:
   void CreateMyMenuItem()
   {
      // submenu item array.
      array<MenuItem^>^ subMenus = gcnew array<MenuItem^>(3);
      // Create three menu items to add to the submenu item array.
      MenuItem^ subMenuItem1 = gcnew MenuItem( "Red" );
      MenuItem^ subMenuItem2 = gcnew MenuItem( "Blue" );
      MenuItem^ subMenuItem3 = gcnew MenuItem( "Green" );
      // Add the submenu items to the array.
      subMenus[ 0 ] = subMenuItem1;
      subMenus[ 1 ] = subMenuItem2;
      subMenus[ 2 ] = subMenuItem3;
      // Create an instance of a MenuItem with caption and an array of submenu
      // items specified.
      MenuItem^ MenuItem1 = gcnew MenuItem( "&Colors",subMenus );
   }
public void CreateMyMenuItem()
{
   // submenu item array.
   MenuItem[] subMenus = new MenuItem[3];
   // Create three menu items to add to the submenu item array.
   MenuItem subMenuItem1 = new MenuItem("Red");
   MenuItem subMenuItem2 = new MenuItem("Blue");
   MenuItem subMenuItem3 = new MenuItem("Green");
   // Add the submenu items to the array.
   subMenus[0] = subMenuItem1;
   subMenus[1] = subMenuItem2;
   subMenus[2] = subMenuItem3;
   // Create an instance of a MenuItem with caption and an array of submenu
   // items specified.
   MenuItem MenuItem1 = new MenuItem("&Colors", subMenus);
}
Public Sub CreateMyMenuItem()
    ' submenu item array.
    Dim subMenus(3) As MenuItem
    ' Create three menu items to add to the submenu item array.
    Dim subMenuItem1 As New MenuItem("Red")
    Dim subMenuItem2 As New MenuItem("Blue")
    Dim subMenuItem3 As New MenuItem("Green")
    ' Add the submenu items to the array.
    subMenus(0) = subMenuItem1
    subMenus(1) = subMenuItem2
    subMenus(2) = subMenuItem3
    ' Create an instance of a MenuItem with caption and an array of submenu
    ' items specified.
    Dim MenuItem1 As New MenuItem("&Colors", subMenus)
End Sub

備註

當您使用 text 參數指定功能表項的 標題 時,您也可以將 『&』 放在做為存取密鑰的字元之前,指定存取鍵。 例如,若要將 “File” 中的 “F” 指定為訪問鍵,您會將功能表項的 標題 指定為 “&File”。 您可以使用此功能來提供選單的鍵盤流覽。

text將 參數設定為 「-」 會導致功能表項顯示為分隔符, (水平線) 而不是標準功能表項。

參數 items 可讓您指派功能表項數位列,以定義此功能表項的子選單。 陣列中的每個專案也可以有指派給它的功能表項陣列。 這可讓您建立完整的功能表結構,並將其指派給功能表項的建構函式。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

MenuItem(String, EventHandler, Shortcut)

使用指定的標題、事件處理常式,和功能表項目的相關快速鍵,來初始化類別的新執行個體。

public:
 MenuItem(System::String ^ text, EventHandler ^ onClick, System::Windows::Forms::Shortcut shortcut);
public MenuItem (string text, EventHandler onClick, System.Windows.Forms.Shortcut shortcut);
new System.Windows.Forms.MenuItem : string * EventHandler * System.Windows.Forms.Shortcut -> System.Windows.Forms.MenuItem
Public Sub New (text As String, onClick As EventHandler, shortcut As Shortcut)

參數

text
String

功能表項目的標題。

onClick
EventHandler

EventHandler,處理這個功能表項目的 Click 事件。

shortcut
Shortcut

其中一個 Shortcut 值。

範例

下列程式代碼範例會建立物件,其中包含指定的 標題、快捷鍵,以及連接到將處理功能表項事件的方法。

public:
   void CreateMyMenuItem()
   {
      // Create a MenuItem with caption, shortcut key, and an event handler
      // specified.
      MenuItem^ MenuItem1 = gcnew MenuItem( "&New",
         gcnew System::EventHandler( this, &Form1::MenuItem1_Click ), Shortcut::CtrlL );
   }

private:
   // The following method is an event handler for menuItem1 to use when
   // connecting the event handler.
   void MenuItem1_Click( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Create a MenuItem with caption, shortcut key, and an event handler
   // specified.
   MenuItem MenuItem1 = new MenuItem("&New",
       new System.EventHandler(this.MenuItem1_Click), Shortcut.CtrlL);
}

// The following method is an event handler for menuItem1 to use when
// connecting the event handler.
private void MenuItem1_Click(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
    ' Create a MenuItem with caption, shortcut key, and an event handler
    ' specified.
    Dim MenuItem1 As New MenuItem("&New", _
       New System.EventHandler(AddressOf Me.MenuItem1_Click), Shortcut.CtrlL)
End Sub    
   
' The following method is an event handler for menuItem1 to use when
' connecting the event handler.
Private Sub MenuItem1_Click(sender As Object, e As EventArgs)
    ' Code goes here that handles the Click event.
End Sub

備註

當您使用 text 參數指定功能表項的 標題 時,您也可以將 『&』 放在做為存取密鑰的字元之前,指定存取鍵。 例如,若要將 “File” 中的 “F” 指定為訪問鍵,您會將功能表項的 標題 指定為 “&File”。 您可以使用此功能來提供選單的鍵盤流覽。 此建構函式也可讓您指定快速鍵,以及提供鍵盤流覽的存取鍵。 快速鍵可讓您指定可用來啟動功能表項的按鍵組合。

text將 參數設定為 「-」 會導致功能表項顯示為分隔符, (水平線) 而不是標準功能表項。

此外,您可以使用這個建構函式來指定委派,以處理 Click 所建立功能表項的事件。 EventHandler您傳遞至這個建構函式的 必須設定為呼叫可處理Click事件的事件處理程式。 如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

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

使用指定的標題;為 MenuItemClickSelect 事件定義的事件處理常式;快速鍵;合併型別;和為功能表項目指定的順序,來初始化 Popup 類別的新執行個體。

public:
 MenuItem(System::Windows::Forms::MenuMerge mergeType, int mergeOrder, System::Windows::Forms::Shortcut shortcut, System::String ^ text, EventHandler ^ onClick, EventHandler ^ onPopup, EventHandler ^ onSelect, cli::array <System::Windows::Forms::MenuItem ^> ^ items);
public MenuItem (System.Windows.Forms.MenuMerge mergeType, int mergeOrder, System.Windows.Forms.Shortcut shortcut, string text, EventHandler onClick, EventHandler onPopup, EventHandler onSelect, System.Windows.Forms.MenuItem[] items);
new System.Windows.Forms.MenuItem : System.Windows.Forms.MenuMerge * int * System.Windows.Forms.Shortcut * string * EventHandler * EventHandler * EventHandler * System.Windows.Forms.MenuItem[] -> System.Windows.Forms.MenuItem
Public Sub New (mergeType As MenuMerge, mergeOrder As Integer, shortcut As Shortcut, text As String, onClick As EventHandler, onPopup As EventHandler, onSelect As EventHandler, items As MenuItem())

參數

mergeType
MenuMerge

其中一個 MenuMerge 值。

mergeOrder
Int32

此功能表項目在合併的功能表中採用的相對位置。

shortcut
Shortcut

其中一個 Shortcut 值。

text
String

功能表項目的標題。

onClick
EventHandler

EventHandler,處理這個功能表項目的 Click 事件。

onPopup
EventHandler

EventHandler,處理這個功能表項目的 Popup 事件。

onSelect
EventHandler

EventHandler,處理這個功能表項目的 Select 事件。

items
MenuItem[]

MenuItem 物件陣列,含有這個功能表項目的子功能表項目。

範例

下列程式代碼範例會建立具有 標題和快捷鍵的功能表項。 功能表項也有針對 PopupClickSelect 事件定義的事件處理程式。 如果合併此功能表項,它會將功能表項新增至功能表,其合併順序為零。

public:
   void CreateMyMenuItem()
   {
      // Submenu item array.
      array<MenuItem^>^ subMenus = gcnew array<MenuItem^>(3);
      // Create three menu items to add to the submenu item array.
      MenuItem^ subMenuItem1 = gcnew MenuItem( "Red" );
      MenuItem^ subMenuItem2 = gcnew MenuItem( "Blue" );
      MenuItem^ subMenuItem3 = gcnew MenuItem( "Green" );
      
      // Add the submenu items to the array.
      subMenus[ 0 ] = subMenuItem1;
      subMenus[ 1 ] = subMenuItem2;
      subMenus[ 2 ] = subMenuItem3;
      /* Create a MenuItem with caption, shortcut key, 
         a Click, Popup, and Select event handler, merge type and order, and an 
         array of submenu items specified.
      */
      MenuItem^ menuItem1 = gcnew MenuItem( MenuMerge::Add, 0,
         Shortcut::CtrlShiftC, "&Colors",
         gcnew EventHandler( this, &Form1::MenuItem1_Click ),
         gcnew EventHandler( this, &Form1::MenuItem1_Popup ),
         gcnew EventHandler( this, &Form1::MenuItem1_Select ), subMenus );
   }

private:
   // The following method is an event handler for menuItem1 to use when connecting the Click event.
   void MenuItem1_Click( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }

   // The following method is an event handler for menuItem1 to use  when connecting the Popup event.
   void MenuItem1_Popup( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }

   // The following method is an event handler for menuItem1 to use  when connecting the Select event
   void MenuItem1_Select( Object^ sender, EventArgs^ e )
   {
      // Code goes here that handles the Click event.
   }
public void CreateMyMenuItem()
{
   // Submenu item array.
   MenuItem[] subMenus = new MenuItem[3];
   // Create three menu items to add to the submenu item array.
   MenuItem subMenuItem1 = new MenuItem("Red");
   MenuItem subMenuItem2 = new MenuItem("Blue");
   MenuItem subMenuItem3 = new MenuItem("Green");

   // Add the submenu items to the array.
   subMenus[0] = subMenuItem1;
   subMenus[1] = subMenuItem2;
   subMenus[2] = subMenuItem3;
   /* Create a MenuItem with caption, shortcut key, 
      a Click, Popup, and Select event handler, merge type and order, and an 
      array of submenu items specified.
   */
   MenuItem menuItem1 = new MenuItem(MenuMerge.Add, 0,
      Shortcut.CtrlShiftC, "&Colors", 
      new EventHandler(this.MenuItem1_Click),
      new EventHandler(this.MenuItem1_Popup),
      new EventHandler(this.MenuItem1_Select), subMenus);
}

// The following method is an event handler for menuItem1 to use when connecting the Click event.
private void MenuItem1_Click(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}

// The following method is an event handler for menuItem1 to use  when connecting the Popup event.
private void MenuItem1_Popup(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}

// The following method is an event handler for menuItem1 to use  when connecting the Select event
private void MenuItem1_Select(Object sender, EventArgs e)
{
   // Code goes here that handles the Click event.
}
Public Sub CreateMyMenuItem()
   ' Submenu item array.
   Dim SubMenus(3) as MenuItem
   ' Create three menu items to add to the submenu item array.
   Dim SubMenuItem1, SubMenuItem2, SubMenuItem3 as MenuItem
   SubMenuItem1 = New MenuItem ("Red")
   SubMenuItem2 = New MenuItem ("Blue")
   SubMenuItem3 = New MenuItem ("Green")
   ' Add the submenu items to the array.
   SubMenus(0) = SubMenuItem1
   SubMenus(1) = SubMenuItem2
   SubMenus(2) = SubMenuItem3
   ' Create a MenuItem with caption, shortcut key, 
   ' a Click, Popup, and Select event handler, menu merge type and order, and an 
   ' array of submenu items specified.
   Dim MenuItem1 As MenuItem
   MenuItem1 = New MenuItem(MenuMerge.Add, 0, Shortcut.CtrlShiftC, "&Colors", _
      AddressOf Me.MenuItem1_Click, _
      AddressOf Me.MenuItem1_Popup, _
      AddressOf Me.MenuItem1_Select, SubMenus)
End Sub

' The following method is an event handler for MenuItem1 to use  when connecting the Click event.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal  e as System.EventArgs)
   ' Code goes here that handles the Click event.
End Sub

' The following method is an event handler for MenuItem1 to use  when connecting the Popup event.
Private Sub MenuItem1_Popup(ByVal sender As System.Object, ByVal  e as System.EventArgs)
   ' Code goes here that handles the Click event.
End Sub

' The following method is an event handler for MenuItem1 to use  when connecting the Select event
Private Sub MenuItem1_Select(ByVal sender As System.Object, ByVal  e as System.EventArgs)
   ' Code goes here that handles the Click event.
End Sub

備註

當您使用 text 參數指定功能表項的 標題 時,您也可以將 『&』 放在做為存取密鑰的字元之前,指定存取鍵。 例如,若要將 “File” 中的 “F” 指定為訪問鍵,您會將功能表項的 標題 指定為 “&File”。 您可以使用此功能來提供選單的鍵盤流覽。

text將 參數設定為 「-」 會導致功能表項顯示為分隔符, (水平線) 而不是標準功能表項。

參數 items 可讓您指派功能表項數位列,以定義此功能表項的子選單。 陣列中的每個專案也可以有指派給它的功能表項陣列。 這可讓您建立完整的功能表結構,並將其指派給功能表項的建構函式。

mergeTypemergeOrder 參數可讓您判斷當功能表項與另一個功能表合併時,此功能表項的行為。 視您為 mergeType 參數指定的值而定,您可以使用它合併的功能表,新增、移除、取代或合併功能表項及其子功能表專案。 參數 mergeOrder 會決定合併功能表時,所建立功能表項的位置。

此外,您可以使用這個建構函式來建立 MenuItem ,並將它連接到程式代碼中的事件處理程式,以處理功能表項的單擊。 EventHandler您傳入這個建構函式的 應該設定為呼叫可處理Click事件的事件處理程式。 藉由使用此建構函式版本,您也可以連接 PopupSelect 事件,以判斷選取此功能表項的時機。 您可以將這些事件用於工作,例如判斷是否要在子功能表專案旁邊顯示複選標記,或根據應用程式的狀態啟用或停用功能表項。 SelectClick 事件只會針對MenuItem不是父功能表項的對象引發。 如需處理事件的詳細資訊,請參閱 處理和引發事件

另請參閱

適用於