Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Control.ContextMenu Property

Gets or sets the shortcut menu associated with the control.

[Visual Basic]
Public Overridable Property ContextMenu As ContextMenu
[C#]
public virtual ContextMenu ContextMenu {get; set;}
[C++]
public: __property virtual ContextMenu* get_ContextMenu();
public: __property virtual void set_ContextMenu(ContextMenu*);
[JScript]
public function get ContextMenu() : ContextMenu;
public function set ContextMenu(ContextMenu);

Property Value

A ContextMenu that represents the shortcut menu associated with the control.

Remarks

A shortcut menu is also known as a context menu. Shortcut menus are used to give context-specific menu options to users when they right-click on the control.

Notes to Inheritors:  When overriding the ContextMenu property in a derived class, use the base class's ContextMenu property to extend the base implementation. Otherwise, you must provide all the implementation. You are not required to override both the get and set accessors of the ContextMenu property; you can override only one if needed.

Example

[Visual Basic, C#, C++] The following example displays the ContextMenu assigned to a TreeView when the right mouse button is clicked and released. This code assumes you have a Form with a TreeView on it. It is also assumed that the TreeView has a ContextMenu assigned to its ContextMenu property.

[Visual Basic] 
Private Sub treeView1_MouseUp(sender As Object, _
  e As MouseEventArgs) Handles treeView1.MouseUp
   ' If the right mouse button was clicked and released,
   ' display the context menu assigned to the TreeView. 
   If e.Button = MouseButtons.Right Then
      treeView1.ContextMenu.Show(treeView1, New Point(e.X, e.Y))
   End If
End Sub

[C#] 
private void treeView1_MouseUp(object sender, MouseEventArgs e)
{
   // If the right mouse button was clicked and released,
   // display the context menu assigned to the TreeView. 
   if(e.Button == MouseButtons.Right)
   {
      treeView1.ContextMenu.Show(treeView1, new Point(e.X, e.Y) );      
   }
}

[C++] 
private:
    void treeView1_MouseUp(Object* /*sender*/, MouseEventArgs* e) {
        // If the right mouse button was clicked and released,
        // display the context menu assigned to the TreeView.
        if (e->Button == MouseButtons::Right) {
            treeView1->ContextMenu->Show(treeView1, Point(e->X, e->Y));
        }
    }

[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, .NET Compact Framework

See Also

Control Class | Control Members | System.Windows.Forms Namespace | ContextMenu

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.