ContextMenu.Popup Evento

Definición

Se produce antes de mostrarse el menú contextual.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 

Tipo de evento

EventHandler

Ejemplos

En el ejemplo de código siguiente se crea un controlador de eventos para el Popup evento de ContextMenu .The following code example creates an event handler for the Popup event of the ContextMenu. El código del controlador de eventos determina cuál de los dos controles con un PictureBox nombre pictureBox1 y un TextBox nombre textBox1 es el control que muestra el menú contextual.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. Dependiendo del control que haya provocado que ContextMenu muestre el menú contextual, el control agrega los MenuItem objetos correspondientes a ContextMenu .Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. Este ejemplo requiere que tenga una instancia de la ContextMenu clase, denominada contextMenu1 , definida dentro del formulario.This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. En este ejemplo también es necesario que tenga un y que se TextBox PictureBox agregue a un formulario y que la ContextMenu propiedad de estos controles esté establecida en contextMenu1 .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

Comentarios

Puede usar este evento para inicializar los MenuItem objetos antes de que se muestren.You can use this event to initialize the MenuItem objects before they are displayed. Por ejemplo, si utiliza un ContextMenu para tres TextBox controles y desea deshabilitar determinados elementos de menú en ContextMenu , dependiendo de que TextBox muestre el menú contextual, puede crear un controlador de eventos para este evento.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. Puede usar la SourceControl propiedad para determinar qué TextBox está a punto de mostrar ContextMenu y deshabilitar los objetos adecuados MenuItem .You could use the SourceControl property to determine which TextBox is about to display the ContextMenu and disable the appropriate MenuItem objects.

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.For more information about handling events, see Handling and Raising Events.

Se aplica a