ContextMenu 建構函式

定義

初始化 ContextMenu 類別的新執行個體。

多載

ContextMenu()

不使用任何指定的功能表項目,初始化 ContextMenu 類別的新執行個體。

ContextMenu(MenuItem[])

使用一組指定 ContextMenu 物件,初始化 MenuItem 類別的新執行個體。

ContextMenu()

不使用任何指定的功能表項目,初始化 ContextMenu 類別的新執行個體。

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

備註

使用這個版本的建構函式之後,您可以使用 類別的 Menu.MenuItemCollection 方法,將功能表項目新增至 。 Add ContextMenu 您可以透過 MenuItems 屬性存取 Menu.MenuItemCollection

另請參閱

適用於

ContextMenu(MenuItem[])

使用一組指定 ContextMenu 物件,初始化 MenuItem 類別的新執行個體。

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 物件的陣列,表示要加入捷徑功能表的功能表項目。

範例

下列程式碼範例示範如何建構快捷方式功能表和使用 Show 方法。 若要執行此範例,請將下列程式碼貼到包含 Button1 按鈕的表單中。 確定所有事件都與其事件處理方法相關聯。

// 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 該建構函式會在建立時指定其功能表項目。 使用這個版本的建構函式之後,您可以使用 類別的 Menu.MenuItemCollection 方法,將其他功能表項目新增至 。 Add ContextMenu 您可以透過 MenuItems 屬性存取 Menu.MenuItemCollection

另請參閱

適用於