Bagikan melalui


ComponentEventHandler Delegasikan

Definisi

Mewakili metode yang akan menangani ComponentAddingperistiwa , , ComponentAddedComponentRemoving, dan ComponentRemoved yang dinaikkan untuk peristiwa tingkat komponen.

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)

Parameter

sender
Object

Sumber dari peristiwa tersebut.

e
ComponentEventArgs

ComponentEventArgs yang berisi data peristiwa.

Atribut

Contoh

Contoh berikut menunjukkan mendaftarkan ComponentEventHandler dan menangani ComponentAddedperistiwa , , ComponentAddingComponentRemoved dan 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

Keterangan

Saat membuat ComponentEventHandler delegasi, Anda mengidentifikasi metode yang akan menangani peristiwa. Untuk mengaitkan peristiwa dengan penanganan aktivitas Anda, tambahkan instans delegasi ke peristiwa. Penanganan aktivitas dipanggil setiap kali peristiwa terjadi, kecuali Jika Anda menghapus delegasi. Untuk informasi selengkapnya tentang delegasi penanganan aktivitas, lihat Menangani dan Meningkatkan Peristiwa.

Metode Ekstensi

GetMethodInfo(Delegate)

Mendapatkan objek yang mewakili metode yang diwakili oleh delegasi yang ditentukan.

Berlaku untuk

Lihat juga