This documentation is archived and is not being maintained.

MenuItem Constructor

.NET Framework 1.1

Initializes a new instance of the MenuItem class.

Overload List

Initializes a MenuItem with a blank caption.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public MenuItem();
[C++] public: MenuItem();
[JScript] public function MenuItem();

Initializes a new instance of the MenuItem class with a specified caption for the menu item.

[Visual Basic] Public Sub New(String)
[C#] public MenuItem(string);
[C++] public: MenuItem(String*);
[JScript] public function MenuItem(String);

Initializes a new instance of the class with a specified caption and event handler for the Click event of the menu item.

[Visual Basic] Public Sub New(String, EventHandler)
[C#] public MenuItem(string, EventHandler);
[C++] public: MenuItem(String*, EventHandler*);
[JScript] public function MenuItem(String, EventHandler);

Initializes a new instance of the class with a specified caption and an array of submenu items defined for the menu item.

[Visual Basic] Public Sub New(String, MenuItem())
[C#] public MenuItem(string, MenuItem[]);
[C++] public: MenuItem(String*, MenuItem[]);
[JScript] public function MenuItem(String, MenuItem[]);

Initializes a new instance of the class with a specified caption, event handler, and associated shortcut key for the menu item.

[Visual Basic] Public Sub New(String, EventHandler, Shortcut)
[C#] public MenuItem(string, EventHandler, Shortcut);
[C++] public: MenuItem(String*, EventHandler*, Shortcut);
[JScript] public function MenuItem(String, EventHandler, Shortcut);

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.

[Visual Basic] Public Sub New(MenuMerge, Integer, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem())
[C#] public MenuItem(MenuMerge, int, Shortcut, string, EventHandler, EventHandler, EventHandler, MenuItem[]);
[C++] public: MenuItem(MenuMerge, int, Shortcut, String*, EventHandler*, EventHandler*, EventHandler*, MenuItem[]);
[JScript] public function MenuItem(MenuMerge, int, Shortcut, String, EventHandler, EventHandler, EventHandler, MenuItem[]);

Example

[Visual Basic, C#, C++] The following example creates a menu item that has a caption and shortcut key. The menu item also has event handlers defined for the Popup, Click, and Select events. If this menu item is merged, it will add the menu item to the menu with the merge order of zero.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the MenuItem constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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


[C#] 
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.
}


[C++] 
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(S"Red");
    MenuItem* subMenuItem2 = new MenuItem(S"Blue");
    MenuItem* subMenuItem3 = new MenuItem(S"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, S"&Colors",
       new EventHandler(this, &Form1::MenuItem1_Click),
       new EventHandler(this, &Form1::MenuItem1_Popup),
       new 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.
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

MenuItem Class | MenuItem Members | System.Windows.Forms Namespace

Show: