Rediger

Del via


ContextMenu Constructors

Definition

Initializes a new instance of the ContextMenu class.

Overloads

ContextMenu()

Initializes a new instance of the ContextMenu class with no menu items specified.

ContextMenu(MenuItem[])

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

ContextMenu()

Initializes a new instance of the ContextMenu class with no menu items specified.

public:
 ContextMenu();
public ContextMenu ();
Public Sub New ()

Remarks

Once you have used this version of the constructor, you can add 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.

See also

Applies to

ContextMenu(MenuItem[])

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

public:
 ContextMenu(cli::array <System::Windows::Forms::MenuItem ^> ^ menuItems);
public ContextMenu (System.Windows.Forms.MenuItem[] menuItems);
new System.Windows.Forms.ContextMenu : System.Windows.Forms.MenuItem[] -> System.Windows.Forms.ContextMenu
Public Sub New (menuItems As MenuItem())

Parameters

menuItems
MenuItem[]

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

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

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.

See also

Applies to