ערוך

שתף באמצעות


ContextMenu.Show Method

Definition

Displays the shortcut menu at the specified position.

Overloads

Show(Control, Point)

Displays the shortcut menu at the specified position.

Show(Control, Point, LeftRightAlignment)

Displays the shortcut menu at the specified position and with the specified alignment.

Show(Control, Point)

Displays the shortcut menu at the specified position.

public:
 void Show(System::Windows::Forms::Control ^ control, System::Drawing::Point pos);
public void Show (System.Windows.Forms.Control control, System.Drawing.Point pos);
member this.Show : System.Windows.Forms.Control * System.Drawing.Point -> unit
Public Sub Show (control As Control, pos As Point)

Parameters

control
Control

A Control that specifies the control with which this shortcut menu is associated.

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

Exceptions

The control parameter is null.

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

Examples

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. 
void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   //Declare the menu items and the shortcut menu.
   array<MenuItem^>^menuItems = {gcnew MenuItem( "Some Button Info" ),gcnew MenuItem( "Some Other Button Info" ),gcnew MenuItem( "Exit" )};
   System::Windows::Forms::ContextMenu^ buttonMenu = gcnew System::Windows::Forms::ContextMenu( menuItems );
   buttonMenu->Show( Button1, System::Drawing::Point( 20, 20 ) );
}

// 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));
}

' Displays the shortcut menu, offsetting its location 
' from the upper-left corner of Button1 by 20 pixels in each direction. 
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    'Declare the menu items and the shortcut menu.
    Dim menuItems() As MenuItem = New MenuItem() _
        {New MenuItem("Some Button Info"), _
        New MenuItem("Some Other Button Info"), _
        New MenuItem("Exit")}

    Dim buttonMenu As New ContextMenu(menuItems)
    buttonMenu.Show(Button1, New System.Drawing.Point(20, 20))
End Sub

Remarks

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.

Applies to

Show(Control, Point, LeftRightAlignment)

Displays the shortcut menu at the specified position and with the specified alignment.

public:
 void Show(System::Windows::Forms::Control ^ control, System::Drawing::Point pos, System::Windows::Forms::LeftRightAlignment alignment);
public void Show (System.Windows.Forms.Control control, System.Drawing.Point pos, System.Windows.Forms.LeftRightAlignment alignment);
member this.Show : System.Windows.Forms.Control * System.Drawing.Point * System.Windows.Forms.LeftRightAlignment -> unit
Public Sub Show (control As Control, pos As Point, alignment As LeftRightAlignment)

Parameters

control
Control

A Control that specifies the control with which this shortcut menu is associated.

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

alignment
LeftRightAlignment

A LeftRightAlignment that specifies the alignment of the control relative to the pos parameter.

Applies to