ComponentEventHandler 委托

定义

表示将处理为组件级事件引发的下列事件的方法:ComponentAddingComponentAddedComponentRemovingComponentRemoved

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)

参数

sender
Object

事件源。

e
ComponentEventArgs

包含事件数据的 ComponentEventArgs

属性

示例

以下示例演示如何注册 ComponentEventHandler 并处理 ComponentAddedComponentAddingComponentRemovedComponentRemoving 事件。

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

注解

创建 ComponentEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅