ContextMenu.Popup 事件

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

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Popup As EventHandler
用法
Dim instance As ContextMenu
Dim handler As EventHandler

AddHandler instance.Popup, handler
public event EventHandler Popup
public:
event EventHandler^ Popup {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_Popup (EventHandler value)

/** @event */
public void remove_Popup (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

可使用该事件在显示 MenuItem 对象之前初始化这些对象。例如,如果对三个 TextBox 控件使用一个 ContextMenu,而且希望在 ContextMenu 中根据正在显示快捷菜单的 TextBox 来禁用某些菜单项,则可为该事件创建事件处理程序。可使用 SourceControl 属性确定将显示 ContextMenuTextBox 和禁用相应的 MenuItem 对象。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例为 ContextMenuPopup 事件创建事件处理程序。事件处理程序中的代码决定以下两个控件中哪一个是显示快捷菜单的控件:名为 pictureBox1PictureBox 和名为 textBox1TextBox。根据哪个控件导致 ContextMenu 显示其快捷菜单,控件向 ContextMenu 添加相应的 MenuItem 对象。此示例要求窗体内定义了一个名为 contextMenu1ContextMenu 类的实例。此示例还要求向窗体添加 TextBoxPictureBox,而且这些控件的 ContextMenu 属性均设置为 contextMenu1

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 '
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:
   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(Object sender, 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.get_MenuItems().Clear();
    if (contextMenu1.get_SourceControl().Equals(textBox1)) {
        // Add MenuItems to display for the TextBox.
        contextMenu1.get_MenuItems().Add(menuItem1);
        contextMenu1.get_MenuItems().Add(menuItem2);
    }
    else {
        if (contextMenu1.get_SourceControl().Equals(pictureBox1)) {
            // Add the MenuItem to display for the PictureBox.
            contextMenu1.get_MenuItems().Add(menuItem3);
        }
    }
} //MyPopupEventHandler

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ContextMenu 类
ContextMenu 成员
System.Windows.Forms 命名空间