MenuItem 构造函数

定义

初始化 MenuItem 类的新实例。

重载

名称 说明
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;为ClickSelect事件和Popup事件定义的事件处理程序;快捷键;合并类型;为菜单项指定的顺序。

MenuItem()

Source:
MenuItem.cs
Source:
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)

Source:
MenuItem.cs
Source:
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 参数为菜单项指定标题时,还可以通过在用作访问键的字符之前放置“>”字符来指定访问键。 例如,若要将“文件”中的“F”指定为访问键,需要将菜单项的标题指定为“&File”。 可以使用此功能为菜单提供键盘导航。

text 参数设置为“-”会导致菜单项显示为分隔符(水平线),而不是标准菜单项。

适用于

MenuItem(String, EventHandler)

Source:
MenuItem.cs
Source:
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 参数为菜单项指定标题时,还可以通过在用作访问键的字符之前放置一个“>”来指定访问键。 例如,若要将“文件”中的“F”指定为访问键,需要将菜单项的标题指定为“&File”。 可以使用此功能为菜单提供键盘导航。

text 参数设置为“-”会导致菜单项显示为分隔符(水平线),而不是标准菜单项。

此外,可以使用此构造函数指定将处理 Click 所创建菜单项的事件的委托。 EventHandler传递给此构造函数的函数必须配置为调用可以处理事件的Click事件处理程序。 有关处理事件的详细信息,请参阅 “处理和引发事件”。

适用于

MenuItem(String, MenuItem[])

Source:
MenuItem.cs
Source:
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

菜单项的标题。

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 参数为菜单项指定标题时,还可以通过在用作访问键的字符之前放置一个“>”来指定访问键。 例如,若要将“文件”中的“F”指定为访问键,需要将菜单项的标题指定为“&File”。 可以使用此功能为菜单提供键盘导航。

text 参数设置为“-”会导致菜单项显示为分隔符(水平线),而不是标准菜单项。

通过此参数 items ,可以分配菜单项数组来定义此菜单项的子菜单。 该数组中的每个项还可以为其分配一组菜单项。 这使你可以创建完整的菜单结构,并将其分配给菜单项的构造函数。

有关处理事件的详细信息,请参阅 “处理和引发事件”。

适用于

MenuItem(String, EventHandler, Shortcut)

Source:
MenuItem.cs
Source:
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 此菜单项的事件。

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 参数为菜单项指定标题时,还可以通过在用作访问键的字符之前放置一个“>”来指定访问键。 例如,若要将“文件”中的“F”指定为访问键,需要将菜单项的标题指定为“&File”。 可以使用此功能为菜单提供键盘导航。 此构造函数还允许除了提供键盘导航的访问键之外,还可以指定快捷键。 使用快捷键可以指定可用于激活菜单项的键的组合。

text 参数设置为“-”会导致菜单项显示为分隔符(水平线),而不是标准菜单项。

此外,可以使用此构造函数指定将处理 Click 所创建菜单项的事件的委托。 EventHandler传递给此构造函数的函数必须配置为调用可以处理事件的Click事件处理程序。 有关处理事件的详细信息,请参阅 “处理和引发事件”。

适用于

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

Source:
MenuItem.cs
Source:
MenuItem.cs

使用指定的标题初始化类的新实例MenuItem;为ClickSelect事件和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 参数为菜单项指定标题时,还可以通过在用作访问键的字符之前放置一个“>”来指定访问键。 例如,若要将“文件”中的“F”指定为访问键,需要将菜单项的标题指定为“&File”。 可以使用此功能为菜单提供键盘导航。

text 参数设置为“-”会导致菜单项显示为分隔符(水平线),而不是标准菜单项。

通过此参数 items ,可以分配菜单项数组来定义此菜单项的子菜单。 该数组中的每个项还可以为其分配一组菜单项。 这使你可以创建完整的菜单结构,并将其分配给菜单项的构造函数。

使用 mergeTypemergeOrder 参数可以确定当菜单项与其他菜单合并时此菜单项的行为方式。 根据为 mergeType 参数指定的值,可以添加、删除、替换或合并菜单项及其子菜单项,并将其与它合并的菜单。 该 mergeOrder 参数确定合并菜单时将放置所创建的菜单项的位置。

此外,可以使用此构造函数创建一个 MenuItem 并将其连接到代码中的事件处理程序,该事件处理程序将处理菜单项的单击。 EventHandler应将传入此构造函数的函数配置为调用可处理事件的Click事件处理程序。 使用此构造函数版本,还可以连接 PopupSelect 事件以确定何时选择此菜单项。 可以将这些事件用于任务,例如确定是否在子菜单项旁边显示复选标记,或者根据应用程序的状态启用或禁用菜单项。 仅Select针对Click不是父菜单项的对象引发和MenuItem事件。 有关处理事件的详细信息,请参阅 “处理和引发事件”。

另请参阅

适用于