Export (0) Print
Expand All

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

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
)
public MenuItem (
	MenuMerge mergeType, 
	int mergeOrder, 
	Shortcut shortcut, 
	String text, 
	EventHandler onClick, 
	EventHandler onPopup, 
	EventHandler onSelect, 
	MenuItem[] items
)
public function MenuItem (
	mergeType : MenuMerge, 
	mergeOrder : int, 
	shortcut : Shortcut, 
	text : String, 
	onClick : EventHandler, 
	onPopup : EventHandler, 
	onSelect : EventHandler, 
	items : MenuItem[]
)

Parameters

mergeType

One of the MenuMerge values.

mergeOrder

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

shortcut

One of the Shortcut values.

text

The caption for the menu item.

onClick

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

onPopup

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

onSelect

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

items

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 Events and Delegates.

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.
}


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.set_Item(0,subMenuItem1);
    subMenus.set_Item(1,subMenuItem2);
    subMenus.set_Item(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);
} //CreateMyMenuItem

// 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.
} //menuItem1_Click

// 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.
} //menuItem1_Popup

// 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.
} //menuItem1_Select

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft