Menu.GetContextMenu 方法

获取包含该菜单的 ContextMenu

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Function GetContextMenu As ContextMenu
用法
Dim instance As Menu
Dim returnValue As ContextMenu

returnValue = instance.GetContextMenu
public ContextMenu GetContextMenu ()
public:
ContextMenu^ GetContextMenu ()
public ContextMenu GetContextMenu ()
public function GetContextMenu () : ContextMenu

返回值

包含该菜单的 ContextMenu。默认为 空引用(在 Visual Basic 中为 Nothing)。

备注

此方法使您得以获取对包含该菜单的 ContextMenu 的引用。如果该菜单没有包含在 ContextMenu 中,则该属性返回 空引用(在 Visual Basic 中为 Nothing)。如果此菜单包含在 MenuItemMainMenu 中,或此菜单没有包含在任何菜单中,则会发生这种情况。可以使用该属性来确定菜单当前是否正被使用,还可以确定在哪里使用。

示例

在此示例中,将使用 GetContextMenu 方法来获得一个对包含 menuItem1menuItem2 的快捷菜单的引用,并将在一个消息框中显示该快捷菜单的信息。可以用编程方式创建包含 NewOpen 两个项的快捷菜单。然后通过生成适当的事件处理程序来给这些项添加功能。运行示例时,将出现一个消息框,告诉您右击窗体可显示快捷菜单。然后,单击菜单项时,将出现另一个消息框,该消息框说明单击了哪个项并显示包含快捷菜单的信息。此示例要求您已创建了一个名为 Form1Form

Public Sub AddContextmenu()
    ' Create a shortcut menu.
    Dim m As New ContextMenu()
    Me.ContextMenu = m

    ' Create MenuItem objects.
    Dim menuItem1 As New MenuItem()
    Dim menuItem2 As New MenuItem()

    ' Set the Text property.
    menuItem1.Text = "New"
    menuItem2.Text = "Open"

    ' Add menu items to the MenuItems collection.
    m.MenuItems.Add(menuItem1)
    m.MenuItems.Add(menuItem2)

    ' Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu items")

    ' Add functionality to the menu items. 
    AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
    AddHandler menuItem2.Click, AddressOf Me.menuItem2_Click

End Sub 'AddContextmenu


Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim textReport As String = "You clicked the New menu item. " + vbCr + "It is contained in the following shortcut menu: " + vbCr + vbCr

    ' Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString()

    ' Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem1_Click


Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim textReport As String = "You clicked the Open menu item. " + vbCr + "It is contained in the following shortcut menu: " + vbCr + vbCr

    ' Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString()

    ' Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem2_Click
public void AddContextmenu()
{
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.ContextMenu= m;

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    
    // Set the Text property.
    menuItem1.Text = "New";
    menuItem2.Text = "Open";

    // Add menu items to the MenuItems collection.
    m.MenuItems.Add(menuItem1);
    m.MenuItems.Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu items");


    // Add functionality to the menu items. 
    menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
    }

private void menuItem1_Click(object sender, System.EventArgs e)
{
    string textReport =    "You clicked the New menu item. \n" +
        "It is contained in the following shortcut menu: \n\n"; 

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport,"The ContextMenu Information");        
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
    string textReport =    "You clicked the Open menu item. \n" +
        "It is contained in the following shortcut menu: \n\n"; 

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport,"The ContextMenu Information");        
}
public:
   [STAThread]
   void AddContextmenu()
   {
      // Create a shortcut menu.
      System::Windows::Forms::ContextMenu^ m = gcnew System::Windows::Forms::ContextMenu;
      this->ContextMenu = m;

      // Create MenuItem objects.
      MenuItem^ menuItem1 = gcnew MenuItem;
      MenuItem^ menuItem2 = gcnew MenuItem;

      // Set the Text property.
      menuItem1->Text = "New";
      menuItem2->Text = "Open";

      // Add menu items to the MenuItems collection.
      m->MenuItems->Add( menuItem1 );
      m->MenuItems->Add( menuItem2 );

      // Display the starting message.
      MessageBox::Show( "Right-click the form to display the shortcut menu items" );

      // Add functionality to the menu items. 
      menuItem1->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );
      menuItem2->Click += gcnew System::EventHandler( this, &Form1::menuItem2_Click );
   }

private:
   void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      String^ textReport = "You clicked the New menu item. \n"
      "It is contained in the following shortcut menu: \n\n";

      // Get information on the shortcut menu in which menuitem1 is contained.
      textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString() );

      // Display the shortcut menu information in a message box.
      MessageBox::Show( textReport, "The ContextMenu Information" );
   }

   void menuItem2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      String^ textReport = "You clicked the Open menu item. \n"
      "It is contained in the following shortcut menu: \n\n";

      // Get information on the shortcut menu in which menuitem1 is contained.
      textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString() );

      // Display the shortcut menu information in a message box.
      MessageBox::Show( textReport, "The ContextMenu Information" );
   }
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.set_ContextMenu(m);

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();

    // Set the Text property.
    menuItem1.set_Text("New");
    menuItem2.set_Text("Open");

    // Add menu items to the MenuItems collection.
    m.get_MenuItems().Add(menuItem1);
    m.get_MenuItems().Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu " 
        + " items");

    // Add functionality to the menu items. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
    menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    String textReport = "You clicked the New menu item. \n" 
        + "It is contained in the following shortcut menu: \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click

private void menuItem2_Click(Object sender, System.EventArgs e)
{
    String textReport = "You clicked the Open menu item. \n" 
        + "It is contained in the following shortcut menu: \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click
public void AddContextmenu()
{
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.set_ContextMenu(m);

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();

    // Set the Text property.
    menuItem1.set_Text("New");
    menuItem2.set_Text("Open");

    // Add menu items to the MenuItems collection.
    m.get_MenuItems().Add(menuItem1);
    m.get_MenuItems().Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu " 
        + " items");

    // Add functionality to the menu items. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
    menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu

private void menuItem1_Click(Object sender, System.EventArgs e)
{
    String textReport = "You clicked the New menu item. \n" 
        + "It is contained in the following shortcut menu: \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click

private void menuItem2_Click(Object sender, System.EventArgs e)
{
    String textReport = "You clicked the Open menu item. \n" 
        + "It is contained in the following shortcut menu: \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Menu 类
Menu 成员
System.Windows.Forms 命名空间
GetMainMenu