This documentation is archived and is not being maintained.

ComponentEventHandler Delegate

Represents the method that will handle the ComponentAdding, ComponentAdded, ComponentRemoving, and ComponentRemoved events raised for component-level events.

[Visual Basic]
<Serializable>
<ComVisible(True)>
Public Delegate Sub ComponentEventHandler( _
   ByVal sender As Object, _
   ByVal e As ComponentEventArgs _
)
[C#]
[Serializable]
[ComVisible(true)]
public delegate void ComponentEventHandler(
   object sender,
   ComponentEventArgs e
);
[C++]
[Serializable]
[ComVisible(true)]
public __gc __delegate void ComponentEventHandler(
   Object* sender,
   ComponentEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the ComponentEventHandler delegate declaration.

sender
The source of the event.
e
A ComponentEventArgs that contains the event data.

Remarks

When you create a ComponentEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

Example

[Visual Basic, C#, C++] The following example demonstrates registering a ComponentEventHandler and handling the ComponentAdded, ComponentAdding, ComponentRemoved and ComponentRemoving events.

[Visual Basic] 
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

[C#] 
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);
}

[C++] 
public:

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

private:

    void OnComponentEvent(Object* /*sender*/, ComponentEventArgs* e)
    {
        // Displays changed component information on the console.
        if( e->Component->Site != 0 )
            Console::WriteLine(S"Name of the component related to the event: {0}", e->Component->Site->Name);
        Console::WriteLine(S"Type of the component related to the event: {0}", e->Component->GetType()->FullName);
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel.Design

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

System.ComponentModel.Design Namespace | ComponentEventArgs | ComponentChangingEventHandler | ComponentChangedEventHandler

Show: