ContextMenu.Popup 事件

定义

在快捷菜单显示之前发生。

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

事件类型

示例

下面的代码示例为 Popup 的 事件 ContextMenu创建事件处理程序。 事件处理程序中的代码确定两个PictureBox命名pictureBox1控件和命名textBox1控件中的哪一个TextBox是显示快捷菜单的控件。 根据哪个控件导致 ContextMenu 显示其快捷菜单,控件会将相应的 MenuItem 对象添加到 。ContextMenu 此示例要求在窗体中定义一个名为 contextMenu1ContextMenu 类的实例。 此示例还要求将 TextBoxPictureBox 添加到窗体, ContextMenu 并且这些控件的 属性设置为 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 。 例如,如果对三TextBox个控件使用 ,ContextMenu并且要根据TextBox显示快捷菜单的 在 中ContextMenu禁用某些菜单项,则可以为此事件创建事件处理程序。 可以使用 SourceControl 属性来确定哪个 TextBox 要显示 ContextMenu 并禁用相应的 MenuItem 对象。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于