ContextMenu ContextMenu ContextMenu ContextMenu Class

Definition

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

public ref class ContextMenu : System::Windows::Forms::Menu
public class ContextMenu : System.Windows.Forms.Menu
type ContextMenu = class
    inherit Menu
Public Class ContextMenu
Inherits Menu
Inheritance

Examples

The following code example creates an event handler for the Popup event of the ContextMenu. The code in the event handler determines which of two controls a PictureBox named pictureBox1 and a TextBox named textBox1 is the control displaying the shortcut menu. Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. This example also requires that you have a TextBox and PictureBox added to a form and that the ContextMenu property of these controls is set to contextMenu1.

private:
   void MyPopupEventHandler( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Define the MenuItem objects to display for the TextBox.
      MenuItem^ menuItem1 = gcnew MenuItem( "&Copy" );
      MenuItem^ menuItem2 = gcnew MenuItem( "&Find and Replace" );
      // Define the MenuItem object to display for the PictureBox.
      MenuItem^ menuItem3 = gcnew MenuItem( "C&hange Picture" );
      
      // Clear all previously added MenuItems.
      contextMenu1->MenuItems->Clear();

      if ( contextMenu1->SourceControl == textBox1 )
      {
         
         // Add MenuItems to display for the TextBox.
         contextMenu1->MenuItems->Add( menuItem1 );
         contextMenu1->MenuItems->Add( menuItem2 );
      }
      else if ( contextMenu1->SourceControl == pictureBox1 )
      {
         // Add the MenuItem to display for the PictureBox.
         contextMenu1->MenuItems->Add( menuItem3 );
      }
   }
private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
 {
    // Define the MenuItem objects to display for the TextBox.
    MenuItem menuItem1 = new MenuItem("&Copy");
    MenuItem menuItem2 = new MenuItem("&Find and Replace");
    // Define the MenuItem object to display for the PictureBox.
    MenuItem menuItem3 = new MenuItem("C&hange Picture");

    // Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear();
 
    if(contextMenu1.SourceControl == textBox1)
    {
       // Add MenuItems to display for the TextBox.
       contextMenu1.MenuItems.Add(menuItem1);
       contextMenu1.MenuItems.Add(menuItem2);
    }
    else if(contextMenu1.SourceControl == pictureBox1)
    {
       // Add the MenuItem to display for the PictureBox.
       contextMenu1.MenuItems.Add(menuItem3);
    }
 }
Private Sub MyPopupEventHandler(sender As System.Object, e As System.EventArgs)
    ' Define the MenuItem objects to display for the TextBox.
    Dim menuItem1 As New MenuItem("&Copy")
    Dim menuItem2 As New MenuItem("&Find and Replace")
    ' Define the MenuItem object to display for the PictureBox.
    Dim menuItem3 As New MenuItem("C&hange Picture")
    
    ' Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear()
    
    If contextMenu1.SourceControl Is textBox1 Then
        ' Add MenuItems to display for the TextBox.
        contextMenu1.MenuItems.Add(menuItem1)
        contextMenu1.MenuItems.Add(menuItem2)
    ElseIf contextMenu1.SourceControl Is pictureBox1 Then
        ' Add the MenuItem to display for the PictureBox.
        contextMenu1.MenuItems.Add(menuItem3)
    End If
End Sub 'MyPopupEventHandler '

Remarks

The 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 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 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 MenuItem objects in a shortcut menu that are not located within a MainMenu to provide situation specific commands that are not appropriate for the 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 Form have a ContextMenu property that binds the ContextMenu class to the control that displays the shortcut menu. More than one control can use a ContextMenu. You can use the 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 Popup event to determine when the shortcut menu is being displayed.

Note

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

Constructors

ContextMenu() ContextMenu() ContextMenu() ContextMenu()

Initializes a new instance of the ContextMenu class with no menu items specified.

ContextMenu(MenuItem[]) ContextMenu(MenuItem[]) ContextMenu(MenuItem[]) ContextMenu(MenuItem[])

Initializes a new instance of the ContextMenu class with a specified set of MenuItem objects.

Properties

CanRaiseEvents CanRaiseEvents CanRaiseEvents CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Container Container Container Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DesignMode DesignMode DesignMode DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Events Events Events Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Handle Handle Handle Handle

Gets a value representing the window handle for the menu.

(Inherited from Menu)
IsParent IsParent IsParent IsParent

Gets a value indicating whether this menu contains any menu items. This property is read-only.

(Inherited from Menu)
MdiListItem MdiListItem MdiListItem MdiListItem

Gets a value indicating the MenuItem that is used to display a list of multiple document interface (MDI) child forms.

(Inherited from Menu)
MenuItems MenuItems MenuItems MenuItems

Gets a value indicating the collection of MenuItem objects associated with the menu.

(Inherited from Menu)
Name Name Name Name

Gets or sets the name of the Menu.

(Inherited from Menu)
RightToLeft RightToLeft RightToLeft RightToLeft

Gets or sets a value indicating whether text displayed by the control is displayed from right to left.

Site Site Site Site

Gets or sets the ISite of the Component.

(Inherited from Component)
SourceControl SourceControl SourceControl SourceControl

Gets the control that is displaying the shortcut menu.

Tag Tag Tag Tag

Gets or sets user-defined data associated with the control.

(Inherited from Menu)

Methods

CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu) CloneMenu(Menu)

Copies the Menu that is passed as a parameter to the current Menu.

(Inherited from Menu)
CreateMenuHandle() CreateMenuHandle() CreateMenuHandle() CreateMenuHandle()

Creates a new handle to the Menu.

(Inherited from Menu)
CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Disposes of the resources, other than memory, used by the Menu.

(Inherited from Menu)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr) FindMenuItem(Int32, IntPtr)

Gets the MenuItem that contains the value specified.

(Inherited from Menu)
FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32) FindMergePosition(Int32)

Returns the position at which a menu item should be inserted into the menu.

(Inherited from Menu)
GetContextMenu() GetContextMenu() GetContextMenu() GetContextMenu()

Gets the ContextMenu that contains this menu.

(Inherited from Menu)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetMainMenu() GetMainMenu() GetMainMenu() GetMainMenu()

Gets the MainMenu that contains this menu.

(Inherited from Menu)
GetService(Type) GetService(Type) GetService(Type) GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu) MergeMenu(Menu)

Merges the MenuItem objects of one menu with the current menu.

(Inherited from Menu)
OnCollapse(EventArgs) OnCollapse(EventArgs) OnCollapse(EventArgs) OnCollapse(EventArgs)

Raises the Collapse event.

OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs) OnPopup(EventArgs)

Raises the Popup event

ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys) ProcessCmdKey(Message, Keys)

Processes a command key.

(Inherited from Menu)
ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control) ProcessCmdKey(Message, Keys, Control)

Processes a command key.

Show(Control, Point) Show(Control, Point) Show(Control, Point) Show(Control, Point)

Displays the shortcut menu at the specified position.

Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment) Show(Control, Point, LeftRightAlignment)

Displays the shortcut menu at the specified position and with the specified alignment.

ToString() ToString() ToString() ToString()

Returns a String that represents the Menu control.

(Inherited from Menu)

Events

Collapse Collapse Collapse Collapse

Occurs when the shortcut menu collapses.

Disposed Disposed Disposed Disposed

Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)
Popup Popup Popup Popup

Occurs before the shortcut menu is displayed.

Fields

FindHandle FindHandle FindHandle FindHandle

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a handle.

(Inherited from Menu)
FindShortcut FindShortcut FindShortcut FindShortcut

Specifies that the FindMenuItem(Int32, IntPtr) method should search for a shortcut.

(Inherited from Menu)

Applies to

See Also