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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public MenuItem(
	MenuMerge mergeType,
	int mergeOrder,
	Shortcut shortcut,
	string text,
	EventHandler onClick,
	EventHandler onPopup,
	EventHandler onSelect,
	MenuItem[] items


Type: System.Windows.Forms.MenuMerge

One of the MenuMerge values.

Type: System.Int32

The relative position that this menu item will take in a merged menu.

Type: System.Windows.Forms.Shortcut

One of the Shortcut values.

Type: System.String

The caption for the menu item.

Type: System.EventHandler

The EventHandler that handles the Click event for this menu item.

Type: System.EventHandler

The EventHandler that handles the Popup event for this menu item.

Type: System.EventHandler

The EventHandler that handles the Select event for this menu item.

Type: System.Windows.Forms.MenuItem[]

An array of MenuItem objects that contains the submenu items for this menu item.

When you specify a caption for your menu item with the text parameter, you can also specify an access key by placing an '&' before the character to be used as the access key. For example, to specify the "F" in "File" as an access key, you would specify the caption for the menu item as "&File". You can use this feature to provide keyboard navigation for your menus.

Setting the text parameter to "-" causes your menu item to be displayed as a separator (a horizontal line) rather than a standard menu item.

The items parameter enables you to assign an array of menu items to define a submenu of this menu item. Each item in the array can also have an array of menu items assigned to it. This enables you to create complete menu structures and assign them to the constructor for the menu item.

The mergeType and mergeOrder parameters allow you to determine how this menu item will behave when the menu item is merged with another menu. Depending on the value you specify for the mergeType parameter, you can either add, remove, replace, or merge the menu item and its submenu items with the menu that it is merging with. The mergeOrder parameter determines where the menu item being created will be positioned when the menu is merged.

In addition, you can use this constructor to create a MenuItem and have it connected to an event handler in your code that will process the click of the menu item. The EventHandler that you pass into this constructor should be configured to call an event handler that can handle the Click event. By using this constructor version, you can also connect the Popup and Select events to determine when this menu item is selected. You can use these events for tasks such as determining whether or not to display a check mark next to submenu items or to enable or disable menu items based on the state of the application. The Select and Click events are raised only for MenuItem objects that are not parent menu items. For more information on handling events, see Handling and Raising Events.

The following code 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.

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.

.NET Framework
Available since 1.1
Return to top