Export (0) Print
Expand All

ContextMenu Constructor (MenuItem[])

Initializes a new instance of the ContextMenu class with a specified set of MenuItem objects.

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

public ContextMenu(
	MenuItem[] menuItems
)

Parameters

menuItems
Type: System.Windows.Forms.MenuItem[]

An array of MenuItem objects that represent the menu items to add to the shortcut menu.

You can use this version of the constructor to create a ContextMenu that has its menu items specified at the time it is created. Once you have used this version of the constructor, you can add additional menu items to the ContextMenu by using the Add method of the Menu.MenuItemCollection class. You can access the Menu.MenuItemCollection through the MenuItems property.

The following code example demonstrates constructing a shortcut menu and using the Show method. To run the example, paste the following code in a form containing a button named Button1. Ensure all events are associated with their event-handling methods.

	
	// Displays the shortcut menu, offsetting its location  
	// from the upper-left corner of Button1 by 20 pixels in each direction.  
	private void Button1_Click(System.Object sender, System.EventArgs e)
	{

		//Declare the menu items and the shortcut menu.
		MenuItem[] menuItems = new MenuItem[]{new MenuItem("Some Button Info"), 
			new MenuItem("Some Other Button Info"), new MenuItem("Exit")};

		ContextMenu buttonMenu = new ContextMenu(menuItems);
		buttonMenu.Show(Button1, new System.Drawing.Point(20, 20));
	}
The following code example demonstrates using the ContextMenu.Show method.
To run the example, paste the following code
in a form containing a button named Button1.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft