This documentation is archived and is not being maintained.

ContextMenu.Popup Event

Occurs before the shortcut menu is displayed.

[Visual Basic]
Public Event Popup As EventHandler
[C#]
public event EventHandler Popup;
[C++]
public: __event EventHandler* Popup;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type EventArgs.

Remarks

You can use this event to initialize the MenuItem objects before they are displayed. For example, if you use a ContextMenu for three TextBox controls and you want to disable certain menu items in the ContextMenu depending on which TextBox is displaying the shortcut menu, you can create an event handler for this event. You could use the SourceControl property to determine which TextBox is about to display the ContextMenu and disable the appropriate MenuItem objects.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following 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 assumes that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. This example also assumes that you have a TextBox and PictureBox added to a form and that the ContextMenu property of these controls is set to contextMenu1.

[Visual Basic] 
Protected 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 '

[C#] 
protected 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);
    }
 }

[C++] 
protected:
void MyPopupEventHandler(System::Object* /*sender*/, System::EventArgs* /*e*/)
 {
    // Define the MenuItem objects to display for the TextBox.
    MenuItem* menuItem1 = new MenuItem(S"&Copy");
    MenuItem* menuItem2 = new MenuItem(S"&Find and Replace");
    // Define the MenuItem object to display for the PictureBox.
    MenuItem* menuItem3 = new MenuItem(S"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);
    }
 }

[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

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

Show: