Menu.GetContextMenu Methode

Definition

Ruft das ContextMenu ab, das dieses Menü enthält.

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

Gibt zurück

Das ContextMenu, das dieses Menü enthält. Der Standardwert ist null.

Beispiele

In diesem Beispiel verwenden Sie die GetContextMenu -Methode, um einen Verweis auf das Kontextmenü abzurufen, das oder menuItem2enthältmenuItem1, und die Kontextmenüinformationen in einem Meldungsfeld anzuzeigen. Sie erstellen programmgesteuert ein Kontextmenü mit zwei Elementen: New und Open. Anschließend fügen Sie diesen Elementen Funktionen hinzu, indem Sie die entsprechenden Ereignishandler erstellen. Wenn Sie das Beispiel ausführen, erhalten Sie ein Meldungsfeld, in dem Sie mit der rechten Maustaste auf das Formular klicken müssen, um das Kontextmenü anzuzeigen. Wenn Sie dann auf ein Menüelement klicken, erhalten Sie eine weitere Meldung, die angibt, auf welches Element geklickt wurde, und zeigt die Informationen im enthaltenden Kontextmenü an. Dieses Beispiel erfordert, dass Sie bereits einen Form namens Form1erstellt haben.

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


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


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

Hinweise

Mit dieser Methode können Sie einen Verweis auf die abrufen, in der ContextMenu sich dieses Menü befindet. Diese Eigenschaft gibt zurück null , wenn das Menü nicht in einem ContextMenuenthalten ist. Dies kann auftreten, wenn das Menü in einem MenuItem oder MainMenuenthalten ist oder wenn das Menü in keinem Menü enthalten ist. Sie können diese Eigenschaft verwenden, um zu bestimmen, ob derzeit ein Menü verwendet wird, und auch, um zu bestimmen, wo.

Gilt für:

Weitere Informationen