ContextMenu Constructor (MenuItem[])
Initializes a new instance of the ContextMenu class with a specified set of MenuItem objects.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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)); }
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.