ContextMenu 构造函数

定义

初始化 ContextMenu 类的新实例。Initializes a new instance of the ContextMenu class.

重载

ContextMenu()

初始化没有任何指定菜单项的 ContextMenu 类的一个新实例。Initializes a new instance of the ContextMenu class with no menu items specified.

ContextMenu(MenuItem[])

使用一组指定的 ContextMenu 对象初始化 MenuItem 类的一个新实例。Initializes a new instance of the ContextMenu class with a specified set of MenuItem objects.

ContextMenu()

初始化没有任何指定菜单项的 ContextMenu 类的一个新实例。Initializes a new instance of the ContextMenu class with no menu items specified.

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

注解

使用此版本的构造函数后,可以使用 @no__t 2 类的 @no__t 方法将菜单项添加到 ContextMenuOnce 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. 可以通过 @no__t 属性访问 Menu.MenuItemCollectionYou can access the Menu.MenuItemCollection through the MenuItems property.

另请参阅

ContextMenu(MenuItem[])

使用一组指定的 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())

参数

menuItems
MenuItem[]

MenuItem 对象的数组,这些对象表示要添加到快捷菜单的菜单项。An array of MenuItem objects that represent the menu items to add to the shortcut menu.

示例

下面的代码示例演示如何构造快捷菜单并使用 Show 方法。The following code example demonstrates constructing a shortcut menu and using the Show method. 若要运行该示例,请将以下代码粘贴到包含名为 Button1 的按钮的窗体中。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

注解

您可以使用此版本的构造函数创建一个 ContextMenu,其菜单项在创建时指定。You can use this version of the constructor to create a ContextMenu that has its menu items specified at the time it is created. 使用此版本的构造函数后,可以使用 @no__t 2 类的 @no__t 方法将其他菜单项添加到 ContextMenuOnce 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. 可以通过 @no__t 属性访问 Menu.MenuItemCollectionYou can access the Menu.MenuItemCollection through the MenuItems property.

另请参阅

适用于