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.

C#
public ContextMenu();

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

.NET Framework 4.8.1 a ďalšie verzie
Produkt Verzie
.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

ContextMenu(MenuItem[])

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

C#
public ContextMenu(System.Windows.Forms.MenuItem[] menuItems);

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.

C#

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

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

.NET Framework 4.8.1 a ďalšie verzie
Produkt Verzie
.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