Export (0) Print
Expand All

Menu.GetContextMenu Method

Gets the ContextMenu that contains this menu.

[Visual Basic]
Public Function GetContextMenu() As ContextMenu
[C#]
public ContextMenu GetContextMenu();
[C++]
public: ContextMenu* GetContextMenu();
[JScript]
public function GetContextMenu() : ContextMenu;

Return Value

The ContextMenu that contains this menu. The default is a null reference (Nothing in Visual Basic).

Remarks

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

Example

[Visual Basic, C#, C++] In this example, you use the method GetContextMenu 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 assumes that you have already created a Form named Form1.

[Visual Basic] 
Public Sub AddContextmenu()
    ' Create a context 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 context 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 context menu: " + vbCr + vbCr

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

    ' Display the context 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 context menu: " + vbCr + vbCr

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

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

[C#] 
public void AddContextmenu()
{
    // Create a context 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 context 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 context menu: \n\n"; 

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

    // Display the context 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 context menu: \n\n"; 

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

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

[C++] 
void AddContextmenu()
{
    // Create a context menu.
    System::Windows::Forms::ContextMenu* m = new System::Windows::Forms::ContextMenu();
    this->ContextMenu= m;

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

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

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

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


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

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

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

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

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

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Menu Class | Menu Members | System.Windows.Forms Namespace | GetMainMenu

Show:
© 2014 Microsoft