This documentation is archived and is not being maintained.

ContextMenuStrip Class

Represents a shortcut menu.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class ContextMenuStrip _
	Inherits ToolStripDropDownMenu
Dim instance As ContextMenuStrip

ContextMenuStrip replaces ContextMenu. You can associate a ContextMenuStrip with any control, and a right mouse click automatically displays the shortcut menu. You can show a ContextMenuStrip programmatically by using the Show method. ContextMenuStrip supports cancelable Opening and Closing events to handle dynamic population and multiple-click scenarios. ContextMenuStrip supports images, menu-item check state, text, access keys, shortcuts, and cascading menus.

The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the ContextMenuStrip control:

Shortcut menus are typically used to combine different menu items from a MenuStrip of a form that are useful for the user given the context of the application. For example, you can use a shortcut menu assigned to a TextBox control to provide menu items for changing the font of the text, finding text within the control, or Clipboard features for copying and pasting text. You can also expose new ToolStripMenuItem objects in a shortcut menu that are not located within a MenuStrip to provide situation-specific commands that are inappropriate for the MenuStrip to display.

Typically, a shortcut menu is displayed when a user clicks the right mouse button over a control or the form itself. Many visible controls, as well as the Form itself, have a Control.ContextMenuStrip property that binds the ContextMenuStrip class to the control that displays the shortcut menu. More than one control can use a ContextMenuStrip.

Set the ToolStripDropDownMenu.ShowCheckMargin property to true to add space to the left of a ToolStripMenuItem for a check mark that shows that the menu item is enabled or selected. The ToolStripDropDownMenu.ShowImageMargin property is set to true by default. Use this space to the left of the ToolStripMenuItem to display an image for that menu item.

Although ContextMenuStrip replaces and adds functionality to the ContextMenu control of previous versions, ContextMenu is retained for both backward compatibility and future use if so desired.

The following code example demonstrates a ContextMenuStrip with dynamic item addition, dynamic SourceControl determination with reuse, and handling of the Opening event.

' This code example demonstrates how to handle the Opening event. 
' It also demonstrates dynamic item addition and dynamic  
' SourceControl determination with reuse. 
Class Form3
    Inherits Form

   ' Declare the ContextMenuStrip control. 
   Private fruitContextMenuStrip As ContextMenuStrip

   Public Sub New()
      ' Create a new ContextMenuStrip control.
      fruitContextMenuStrip = New ContextMenuStrip()

      ' Attach an event handler for the  
      ' ContextMenuStrip control's Opening event. 
      AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening

      ' Create a new ToolStrip control. 
      Dim ts As New ToolStrip()

      ' Create a ToolStripDropDownButton control and add it 
      ' to the ToolStrip control's Items collections. 
      Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")

      ' Dock the ToolStrip control to the top of the form.
      ts.Dock = DockStyle.Top

      ' Assign the ContextMenuStrip control as the  
      ' ToolStripDropDownButton control's DropDown menu.
      fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

      ' Create a new MenuStrip control and add a ToolStripMenuItem. 
      Dim ms As New MenuStrip()
      Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")

      ' Dock the MenuStrip control to the top of the form.
      ms.Dock = DockStyle.Top

      ' Assign the MenuStrip control as the  
      ' ToolStripMenuItem's DropDown menu.
      fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

      ' Assign the ContextMenuStrip to the form's  
      ' ContextMenuStrip property. 
      Me.ContextMenuStrip = fruitContextMenuStrip

      ' Add the ToolStrip control to the Controls collection. 

        'Add a button to the form and assign its ContextMenuStrip. 
        Dim b As New Button()
        b.Location = New System.Drawing.Point(60, 60)
        b.ContextMenuStrip = fruitContextMenuStrip

      ' Add the MenuStrip control last. 
      ' This is important for correct placement in the z-order. 
    End Sub 

   ' This event handler is invoked when the ContextMenuStrip 
   ' control's Opening event is raised. It demonstrates 
   ' dynamic item addition and dynamic SourceControl  
   ' determination with reuse. 
    Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

        ' Acquire references to the owning control and item. 
        Dim c As Control = fruitContextMenuStrip.SourceControl
        Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem 

        ' Clear the ContextMenuStrip control's  
        ' Items collection.

        ' Check the source control first. 
        If (c IsNot Nothing) Then 
            ' Add custom item (Form)
            fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))
        ElseIf (tsi IsNot Nothing) Then 
            ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
            fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))
        End If 

        ' Populate the ContextMenuStrip control with its default items.

        ' Set Cancel to false.  
        ' It is optimized to true based on empty entry.
        e.Cancel = False 
    End Sub 
End Class


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0