ComponentEventHandler 委托

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

**命名空间:**System.ComponentModel.Design
**程序集:**System(在 system.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public Delegate Sub ComponentEventHandler ( _
    sender As Object, _
    e As ComponentEventArgs _
)
用法
Dim instance As New ComponentEventHandler(AddressOf HandlerMethod)
[ComVisibleAttribute(true)] 
public delegate void ComponentEventHandler (
    Object sender,
    ComponentEventArgs e
)
[ComVisibleAttribute(true)] 
public delegate void ComponentEventHandler (
    Object^ sender, 
    ComponentEventArgs^ e
)
/** @delegate */
/** @attribute ComVisibleAttribute(true) */ 
public delegate void ComponentEventHandler (
    Object sender, 
    ComponentEventArgs e
)
JScript 支持使用委托,但不支持进行新的声明。

参数

  • sender
    事件源。

备注

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

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:SharedStateHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

示例

下面的示例说明如何注册 ComponentEventHandler 和处理 ComponentAddedComponentAddingComponentRemoved 以及 ComponentRemoving 事件。

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 Not (e.Component.Site Is Nothing) Then
        Console.WriteLine(("Name of the component related to the event: " + e.Component.Site.Name))
    End If
End Sub
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:
   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.add_ComponentAdded(new ComponentEventHandler(this.
        OnComponentEvent));
    changeService.add_ComponentAdding(new ComponentEventHandler(this.
        OnComponentEvent));
    changeService.add_ComponentRemoved(new ComponentEventHandler(this.
        OnComponentEvent));
    changeService.add_ComponentRemoving(new ComponentEventHandler(this.
        OnComponentEvent));
} //LinkComponentEvent

private void OnComponentEvent(Object sender, ComponentEventArgs e)
{
    // Displays changed component information on the console.            
    if (e.get_Component().get_Site() != null) {
        Console.WriteLine("Name of the component related to the event: "
            + e.get_Component().get_Site().get_Name());
    }
    Console.WriteLine("Type of the component related to the event: "
        + e.get_Component().GetType().get_FullName());
} //OnComponentEvent

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、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

请参见

参考

System.ComponentModel.Design 命名空间
ComponentEventArgs 类
ComponentChangingEventHandler 委托
ComponentChangedEventHandler 委托