ComponentEventHandler Delegat

Definicja

Reprezentuje metodę, która będzie obsługiwać ComponentAddingzdarzenia , , ComponentAddedComponentRemovingi ComponentRemoved zgłaszane dla zdarzeń na poziomie składnika.

public delegate void ComponentEventHandler(System::Object ^ sender, ComponentEventArgs ^ e);
public delegate void ComponentEventHandler(object sender, ComponentEventArgs e);
public delegate void ComponentEventHandler(object? sender, ComponentEventArgs e);
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ComponentEventHandler(object sender, ComponentEventArgs e);
type ComponentEventHandler = delegate of obj * ComponentEventArgs -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
type ComponentEventHandler = delegate of obj * ComponentEventArgs -> unit
Public Delegate Sub ComponentEventHandler(sender As Object, e As ComponentEventArgs)

Parametry

sender
Object

Źródło zdarzenia.

e
ComponentEventArgs

Element ComponentEventArgs zawierający dane zdarzenia.

Atrybuty

Przykłady

W poniższym przykładzie pokazano rejestrowanie ComponentEventHandler i obsługę zdarzeń ComponentAdded, ComponentAddingComponentRemoved i ComponentRemoving .

public:
   void LinkComponentEvent( IComponentChangeService^ changeService )
   {
      // Registers an event handler for the ComponentAdded,
      // ComponentAdding, ComponentRemoved, and ComponentRemoving events.
      changeService->ComponentAdded += gcnew ComponentEventHandler(
         this, &ComponentEventHandlerExample::OnComponentEvent );
      changeService->ComponentAdding += gcnew ComponentEventHandler(
         this, &ComponentEventHandlerExample::OnComponentEvent );
      changeService->ComponentRemoved += gcnew ComponentEventHandler(
         this, &ComponentEventHandlerExample::OnComponentEvent );
      changeService->ComponentRemoving += gcnew ComponentEventHandler(
         this, &ComponentEventHandlerExample::OnComponentEvent );
   }

private:
   void OnComponentEvent( Object^ sender, ComponentEventArgs^ e )
   {
      // Displays changed component information on the console.
      if ( e->Component->Site != nullptr )
      {
         Console::WriteLine( "Name of the component related to the event: " +
            e->Component->Site->Name );
      }
      Console::WriteLine( "Type of the component related to the event: " +
         e->Component->GetType()->FullName );
   }
public void LinkComponentEvent(IComponentChangeService changeService)
{
    // Registers an event handler for the ComponentAdded,
    // ComponentAdding, ComponentRemoved, and ComponentRemoving events.
    changeService.ComponentAdded += new ComponentEventHandler(this.OnComponentEvent);            
    changeService.ComponentAdding += new ComponentEventHandler(this.OnComponentEvent);            
    changeService.ComponentRemoved += new ComponentEventHandler(this.OnComponentEvent);            
    changeService.ComponentRemoving += new ComponentEventHandler(this.OnComponentEvent);                        
}

private void OnComponentEvent(object sender, ComponentEventArgs e)
{
    // Displays changed component information on the console.            
    if( e.Component.Site != null )
        Console.WriteLine("Name of the component related to the event: "+e.Component.Site.Name);      
    Console.WriteLine("Type of the component related to the event: "+e.Component.GetType().FullName);
}
Public Sub LinkComponentEvent(ByVal changeService As IComponentChangeService)
    ' Registers an event handler for the ComponentAdded,
    ' ComponentAdding, ComponentRemoved, and ComponentRemoving events.
    AddHandler changeService.ComponentAdded, AddressOf Me.OnComponentEvent
    AddHandler changeService.ComponentAdding, AddressOf Me.OnComponentEvent
    AddHandler changeService.ComponentRemoved, AddressOf Me.OnComponentEvent
    AddHandler changeService.ComponentRemoving, AddressOf Me.OnComponentEvent
End Sub

Private Sub OnComponentEvent(ByVal sender As Object, ByVal e As ComponentEventArgs)
    ' Displays changed component information on the console.            
    If (e.Component.Site IsNot Nothing) Then
        Console.WriteLine(("Name of the component related to the event: " + e.Component.Site.Name))
    End If
End Sub

Uwagi

Podczas tworzenia delegata należy zidentyfikować metodę ComponentEventHandler , która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Metody rozszerzania

GetMethodInfo(Delegate)

Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata.

Dotyczy

Zobacz też