Menu.GetContextMenu Method

Definition

Gets the ContextMenu that contains this menu.

C#
public System.Windows.Forms.ContextMenu GetContextMenu();

Returns

The ContextMenu that contains this menu. The default is null.

Examples

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. You 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. This example requires that you have already created a Form named Form1.

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

Remarks

This method allows you to obtain a reference to the ContextMenu that this menu is contained in. This property returns null if the menu is not contained in a ContextMenu. 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.

Applies to

Produkt Verzie
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

See also