MenuItem Constructors

Definition

Initializes a new instance of the MenuItem class.

Overloads

MenuItem()

Initializes a MenuItem with a blank caption.

MenuItem(String)

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

MenuItem(String, EventHandler)

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

MenuItem(String, MenuItem[])

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

MenuItem(String, EventHandler, Shortcut)

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

MenuItem(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.

MenuItem()

Initializes a MenuItem with a blank caption.

C#
public MenuItem();

Examples

The following code example creates a MenuItem using this version of the constructor.

C#
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";
}

Remarks

Once you have created a blank MenuItem using this constructor, you can use the properties and methods of the MenuItem class to specify the appearance and behavior of your MenuItem.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

MenuItem(String)

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

C#
public MenuItem(string text);

Parameters

text
String

The caption for the menu item.

Examples

The following code example creates a MenuItem that specifies the caption of the menu item at the time it is constructed.

C#
public void CreateMyMenus()
{
   // Create an instance of a MenuItem with a specified caption.
   menuItem1 = new MenuItem("&File");
}

Remarks

When you specify a caption for your menu item with the text parameter, you can also specify an access key by placing an '&' character 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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

MenuItem(String, EventHandler)

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

C#
public MenuItem(string text, EventHandler onClick);

Parameters

text
String

The caption for the menu item.

onClick
EventHandler

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

Examples

The following code example creates a MenuItem object with a specified caption and an EventHandler delegate connected to an event handler that will handle the Click event for the menu item.

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

Remarks

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.

In addition, you can use this constructor to specify a delegate that will handle the Click event for the menu item being created. The EventHandler that you pass to this constructor must be configured to call an event handler that can handle the Click event. For more information on handling events, see Handling and Raising Events.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

MenuItem(String, MenuItem[])

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

C#
public MenuItem(string text, System.Windows.Forms.MenuItem[] items);

Parameters

text
String

The caption for the menu item.

items
MenuItem[]

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

Examples

The following code example creates an object with a specified caption, an event handler connected to a method that will handle the event each menu item in an array of submenu items.

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 an instance of a MenuItem with caption and an array of submenu
   // items specified.
   MenuItem MenuItem1 = new MenuItem("&Colors", subMenus);
}

Remarks

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.

For more information on handling events, see Handling and Raising Events.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

MenuItem(String, EventHandler, Shortcut)

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

C#
public MenuItem(string text, EventHandler onClick, System.Windows.Forms.Shortcut shortcut);

Parameters

text
String

The caption for the menu item.

onClick
EventHandler

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

shortcut
Shortcut

One of the Shortcut values.

Examples

The following code example creates an object with a specified caption, shortcut key, and an event handler connected to a method that will handle the event for the menu item.

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

Remarks

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. This constructor also enables you to specify a shortcut key in addition to an access key to provide keyboard navigation. Shortcut keys allow you to specify a combination of keys that can be used to activate the menu item.

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

In addition, you can use this constructor to specify a delegate that will handle the Click event for the menu item being created. The EventHandler that you pass to this constructor must be configured to call an event handler that can handle the Click event. For more information on handling events, see Handling and Raising Events.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

MenuItem(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.

C#
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);

Parameters

mergeType
MenuMerge

One of the MenuMerge values.

mergeOrder
Int32

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

shortcut
Shortcut

One of the Shortcut values.

text
String

The caption for the menu item.

onClick
EventHandler

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

onPopup
EventHandler

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

onSelect
EventHandler

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

items
MenuItem[]

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

Examples

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.

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

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10