ContextMenu.Popup 事件

定义

在快捷菜单显示之前发生。Occurs before the shortcut menu is displayed.

public:
 event EventHandler ^ Popup;
public event EventHandler Popup;
member this.Popup : EventHandler 
Public Custom Event Popup As EventHandler 

示例

下面的代码示例为 ContextMenuPopup 事件创建事件处理程序。The following code example creates an event handler for the Popup event of the ContextMenu. 事件处理程序中的代码确定两个控件中的哪个控件 PictureBox 名为 pictureBox1,一个名为 textBox1 TextBox 为控件,该控件显示快捷菜单。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. 控件将适当的 MenuItem 对象添加到 ContextMenu,具体取决于哪个控件导致 ContextMenu 显示其快捷菜单。Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. 此示例要求在窗体中定义一个名为 contextMenu1ContextMenu 类的实例。This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. 此示例还要求将 TextBoxPictureBox 添加到窗体,并将这些控件的 ContextMenu 属性设置为 contextMenu1This 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

注解

在显示 MenuItem 对象之前,可以使用此事件对其进行初始化。You can use this event to initialize the MenuItem objects before they are displayed. 例如,如果对三个 TextBox 控件使用 ContextMenu,并且想要根据显示快捷菜单的 TextBox 来禁用 ContextMenu 中的某些菜单项,则可以为此事件创建事件处理程序。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. 您可以使用 SourceControl 属性来确定哪些 TextBox 将要显示 ContextMenu 并禁用适当的 MenuItem 对象。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 Handling and Raising Events.

适用于