This topic has not yet been rated - Rate this topic

ContextMenu.Show Method (Control, Point)

Displays the shortcut menu at the specified position.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public void Show(
	Control control,
	Point pos
)

Parameters

control
Type: System.Windows.Forms.Control
A Control that specifies the control with which this shortcut menu is associated.
pos
Type: System.Drawing.Point
A Point that specifies the coordinates at which to display the menu. These coordinates are specified relative to the client coordinates of the control specified in the control parameter.
Exception Condition
ArgumentNullException

The control parameter is null.

ArgumentException

The handle of the control does not exist or the control is not visible.

Typically, a ContextMenu is displayed when the user clicks the right mouse button on a control or area of the form that the ContextMenu is bound to. You can use this method to manually display the shortcut menu at a specific location and bind it with a specific control. This method does not return until the menu is dismissed.

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, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ