Menu.GetContextMenu Menu.GetContextMenu Menu.GetContextMenu Menu.GetContextMenu Method

定义

获取包含该菜单的 ContextMenuGets the ContextMenu that contains this menu.

public:
 System::Windows::Forms::ContextMenu ^ GetContextMenu();
public System.Windows.Forms.ContextMenu GetContextMenu ();
member this.GetContextMenu : unit -> System.Windows.Forms.ContextMenu
Public Function GetContextMenu () As ContextMenu

返回

包含该菜单的 ContextMenuThe ContextMenu that contains this menu. 默认值为 nullThe default is null.

示例

在此示例中,使用GetContextMenu方法来获取对包含的快捷菜单的引用menuItem1menuItem2,并在消息框中显示的快捷方式菜单信息。In this example, you use the GetContextMenu method to obtain a reference to the shortcut menu that contains menuItem1 or menuItem2, and display the shortcut menu information in a message box. 以编程方式创建包含两个项的快捷菜单NewOpenYou programmatically create a shortcut menu with two items, New and Open. 然后将功能对这些项添加通过创建相应的事件处理程序。You then add functionality to these items by creating the appropriate event handlers. 运行该示例,可以获取一个消息框,告诉你右键单击该表单,以显示快捷菜单。When you run the example, you get a message box that tells you to right-click the form in order to display the shortcut menu. 当单击菜单项,你将获取另一条消息,指出哪一项已被单击和包含的快捷菜单上显示的信息。Then, when you click a menu item, you get another message that tells which item has been clicked and displays the information on the containing shortcut menu. 此示例需要已创建Form名为Form1This example requires that you have already created a Form named Form1.

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

注解

此方法,可获取对引用ContextMenu中包含此菜单。This method allows you to obtain a reference to the ContextMenu that this menu is contained in. 此属性返回null如果菜单未包含在ContextMenuThis property returns null if the menu is not contained in a ContextMenu. 发生这种情况中包含菜单MenuItemMainMenu,或如果菜单未包含在任何菜单。This can occur if the menu is contained in a MenuItem or MainMenu, or if the menu is not contained in any menu. 若要确定是否当前正在使用一个菜单,以及确定在何处,可以使用此属性。You can use this property to determine whether a menu is currently being used, and also to determine where.

适用于

另请参阅