System.Windows.Forms.ContextMenu Class

Represents a shortcut menu. Although System.Windows.Forms.ContextMenuStrip replaces and adds functionality to the System.Windows.Forms.ContextMenu control of previous versions, System.Windows.Forms.ContextMenu is retained for both backward compatibility and future use if you choose.

See Also: ContextMenu Members

Syntax

[System.ComponentModel.DefaultEvent("Popup")]
public class ContextMenu : Menu

Remarks

The System.Windows.Forms.ContextMenu class represents shortcut menus that can be displayed when the user clicks the right mouse button over a control or area of the form. Shortcut menus are typically used to combine different menu items from a System.Windows.Forms.MainMenu 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 System.Windows.Forms.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 display new System.Windows.Forms.MenuItem objects in a shortcut menu that are not located within a System.Windows.Forms.MainMenu to provide situation specific commands that are not appropriate for the System.Windows.Forms.MainMenu to display.

Typically, a shortcut menu is displayed when a user clicks the right mouse button over a control or the form itself. Visible controls and System.Windows.Forms.Form have a Control.ContextMenu property that binds the System.Windows.Forms.ContextMenu class to the control that displays the shortcut menu. More than one control can use a System.Windows.Forms.ContextMenu. You can use the ContextMenu.SourceControl property to determine which control last displayed the shortcut menu in order to perform tasks specific to the control or to modify the shortcut menu displayed for the control.

You might want to know when the shortcut menu is being displayed in order to set check marks, disable items, and perform other menu tasks before the menu is displayed to the user. You can handle the ContextMenu.Popup event to determine when the shortcut menu is being displayed.

Note:

In order to reuse System.Windows.Forms.MenuItem objects that are displayed in a System.Windows.Forms.MainMenu for use in a System.Windows.Forms.ContextMenu, you must create a copy of the menu using the MenuItem.CloneMenu method of the System.Windows.Forms.MenuItem class. You can also merge menu items and their submenu items into a single System.Windows.Forms.MenuItem object using the MenuItem.MergeMenu method of the System.Windows.Forms.MenuItem class.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0