MenuItem 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 MenuItem 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| MenuItem() |
初始化 a MenuItem 並附上空白說明。 |
| MenuItem(String) |
初始化一個新的類別實例 MenuItem ,並指定選單項目的說明文字。 |
| MenuItem(String, EventHandler) |
初始化一個新的類別實例,並設定指定的標題與事件處理程序,以處理 Click 選單項目的事件。 |
| MenuItem(String, MenuItem[]) |
初始化一個新的類別實例,並以指定的說明文字及定義的子選單項目陣列。 |
| MenuItem(String, EventHandler, Shortcut) |
初始化一個新的類別實例,並附上指定的說明文字、事件處理程序及選單項目的快捷鍵。 |
| MenuItem(MenuMerge, Int32, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[]) |
初始化類別的新實例MenuItem,並指定說明文字;定義事件處理器(事件處理程序)、ClickSelectPopup事件;快捷鍵;合併類型;以及選單項目指定的排序。 |
MenuItem()
- 來源:
- MenuItem.cs
- 來源:
- MenuItem.cs
初始化 a 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.cs
- 來源:
- MenuItem.cs
初始化一個新的類別實例 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)
- 來源:
- MenuItem.cs
- 來源:
- MenuItem.cs
初始化一個新的類別實例,並設定指定的標題與事件處理程序,以處理 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[])
- 來源:
- MenuItem.cs
- 來源:
- MenuItem.cs
初始化一個新的類別實例,並以指定的說明文字及定義的子選單項目陣列。
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
菜單項目的說明。
範例
以下程式碼範例會建立一個帶有指定說明的物件,一個事件處理器連接到一個方法,該方法會處理子選單項目陣列中的每個選單項目的事件。
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)
- 來源:
- MenuItem.cs
- 來源:
- MenuItem.cs
初始化一個新的類別實例,並附上指定的說明文字、事件處理程序及選單項目的快捷鍵。
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 處理這個選單項目的事件。
範例
以下程式碼範例建立一個物件,包含指定的說明文字、快捷鍵,以及一個事件處理器,並連接到一個能處理選單項目事件的方法。
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[])
- 來源:
- MenuItem.cs
- 來源:
- MenuItem.cs
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())
參數
- mergeOrder
- Int32
這個選單項目在合併選單中所佔的位置。
- text
- String
菜單項目的說明。
- onClick
- EventHandler
那 EventHandler 個負責 Click 處理這個選單項目的事件。
- onPopup
- EventHandler
那 EventHandler 個負責 Popup 處理這個選單項目的事件。
- onSelect
- EventHandler
那 EventHandler 個負責 Select 處理這個選單項目的事件。
範例
以下程式碼範例會建立一個包含說明文字和快捷鍵的選單項目。 選單項目中也定義了事件處理程序,分別用於 Popup、 Click和 Select 事件。 如果這個選單項目被合併,它會將該選單項目以合併順序為零的方式加入選單。
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 參數讓你能指派一個選單項目陣列,以定義該選單項目的子選單。 陣列中的每個項目也可以被指派一個選單項目陣列。 這讓你能建立完整的選單結構,並將其指派給選單項目的建構者。
mergeType和mergeOrder參數可以讓你判斷當該選單項目與另一個選單合併時的行為。 根據你設定 mergeType 的參數值,你可以新增、移除、替換或合併選單項目及其子選單項目與它合併的選單。 參數 mergeOrder 決定合併選單時所建立的選單項目會放在哪裡。
此外,你也可以使用這個建構子建立一個 MenuItem ,並將其連接到程式碼中的事件處理程序,該處理器會處理選單項目的點擊。 你傳遞到這個建構子的 應該 EventHandler 設定為呼叫一個能處理事件的 Click 事件處理器。 使用這個建構版本,你也可以連接 Popup 和 Select 事件,以判斷選單項目何時被選中。 你可以利用這些事件來執行任務,例如判斷是否要在子選單項目旁顯示勾選,或根據應用程式狀態啟用或停用選單項目。 Select且Click事件僅會針對MenuItem非父選單項目的物件觸發。 欲了解更多有關事件處理的資訊,請參閱 「處理與提升事件」。