.NET Framework Class Library
IComponentChangeService Interface

Provides an interface to add and remove the event handlers for events that add, change, remove or rename components, and provides methods to raise a ComponentChanged or ComponentChanging event.

Namespace:   System.ComponentModel.Design
Assembly:  System (in System.dll)
Syntax
<[%$TOPIC/22dytfsd_en-us_VS_110_3_0_0_0_0%](True)> _
Public Interface IComponentChangeService
[[%$TOPIC/22dytfsd_en-us_VS_110_3_0_1_0_0%](true)]
public interface IComponentChangeService
[[%$TOPIC/22dytfsd_en-us_VS_110_3_0_2_0_0%](true)]
public interface class IComponentChangeService
[<[%$TOPIC/22dytfsd_en-us_VS_110_3_0_3_0_0%](true)>]
type IComponentChangeService =  interface end

The IComponentChangeService type exposes the following members.

Methods
  NameDescription
Public method OnComponentChangedAnnounces to the component change service that a particular component has changed.
Public method OnComponentChangingAnnounces to the component change service that a particular component is changing.
Top
Events
  NameDescription
Public event ComponentAddedOccurs when a component has been added.
Public event ComponentAddingOccurs when a component is in the process of being added.
Public event ComponentChangedOccurs when a component has been changed.
Public event ComponentChangingOccurs when a component is in the process of being changed.
Public event ComponentRemovedOccurs when a component has been removed.
Public event ComponentRemovingOccurs when a component is in the process of being removed.
Public event ComponentRenameOccurs when a component is renamed.
Top
Remarks

IComponentChangeService provides an interface that can be used to indicate the methods that handle the following events:

Typically, the design environment raises these component add, change, remove, or rename events. Designers should call the methods of this interface when using DesignerTransaction objects to provide undo and redo functionality for design-time actions that affect components. More information is available in the documentation for DesignerTransaction. Generally, only the root designer handles these change notifications.

This service also provides methods that raise a component changed event or component changing event. A PropertyDescriptor or a component can indicate that a component has changed or is changing with the OnComponentChanged and OnComponentChanging methods, respectively.

Examples

This following example demonstrates how to use the IComponentChangeService interface to receive notifications about the addition of, removal of, and changes to components in design mode.

Imports System
Imports System.Data
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms

'  This sample illustrates how to use the IComponentChangeService interface  
'    to handle component change events.  The ComponentClass control attaches  
'    event handlers when it is sited in a document, and displays a message  
'    when notification that a component has been added, removed, or changed 
'    is received from the IComponentChangeService. 

'    To run this sample, add the ComponentClass control to a Form and 
'    add, remove, or change components to see the behavior of the 
'    component change event handlers.  

Namespace IComponentChangeServiceExample
    _
   Public Class ComponentClass
      Inherits System.Windows.Forms.UserControl
      Private components As System.ComponentModel.Container = Nothing 
      Private listBox1 As System.Windows.Forms.ListBox
      Private m_changeService As IComponentChangeService    

      Public Sub New()
         InitializeComponent()
        End Sub 

        Private Sub InitializeComponent()
            Me.listBox1 = New System.Windows.Forms.ListBox()
            Me.SuspendLayout()

            ' listBox1. 
            Me.listBox1.Location = New System.Drawing.Point(24, 16)
            Me.listBox1.Name = "listBox1" 
            Me.listBox1.Size = New System.Drawing.Size(576, 277)
            Me.listBox1.TabIndex = 0

            ' ComponentClass. 
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1})
            Me.Name = "ComponentClass" 
            Me.Size = New System.Drawing.Size(624, 320)

            Me.ResumeLayout(False)
        End Sub 

        ' This override allows the control to register event handlers for IComponentChangeService events 
        ' at the time the control is sited, which happens only in design mode. 
        Public Overrides Property Site() As ISite
            Get 
                Return MyBase.Site
            End Get 
            Set(ByVal Value As ISite)
                ' Clear any component change event handlers.
                ClearChangeNotifications()

                ' Set the new Site value. 
                MyBase.Site = Value

                m_changeService = CType(GetService(GetType(IComponentChangeService)), IComponentChangeService)

                ' Register event handlers for component change events.
                RegisterChangeNotifications()
            End Set 
        End Property 

        Private Sub ClearChangeNotifications()
            ' The m_changeService value is null when not in design mode,  
            ' as the IComponentChangeService is only available at design time.	
            m_changeService = CType(GetService(GetType(IComponentChangeService)), IComponentChangeService)

            ' Clear our the component change events to prepare for re-siting.				 
            If (m_changeService IsNot Nothing) Then 
                RemoveHandler m_changeService.ComponentChanged, AddressOf OnComponentChanged
                RemoveHandler m_changeService.ComponentChanging, AddressOf OnComponentChanging
                RemoveHandler m_changeService.ComponentAdded, AddressOf OnComponentAdded
                RemoveHandler m_changeService.ComponentAdding, AddressOf OnComponentAdding
                RemoveHandler m_changeService.ComponentRemoved, AddressOf OnComponentRemoved
                RemoveHandler m_changeService.ComponentRemoving, AddressOf OnComponentRemoving
                RemoveHandler m_changeService.ComponentRename, AddressOf OnComponentRename
            End If 
        End Sub 

        Private Sub RegisterChangeNotifications()
            ' Register the event handlers for the IComponentChangeService events 
            If (m_changeService IsNot Nothing) Then 
                AddHandler m_changeService.ComponentChanged, AddressOf OnComponentChanged
                AddHandler m_changeService.ComponentChanging, AddressOf OnComponentChanging
                AddHandler m_changeService.ComponentAdded, AddressOf OnComponentAdded
                AddHandler m_changeService.ComponentAdding, AddressOf OnComponentAdding
                AddHandler m_changeService.ComponentRemoved, AddressOf OnComponentRemoved
                AddHandler m_changeService.ComponentRemoving, AddressOf OnComponentRemoving
                AddHandler m_changeService.ComponentRename, AddressOf OnComponentRename
            End If 
        End Sub 

        ' This method handles the OnComponentChanged event to display a notification.  
        Private Sub OnComponentChanged(ByVal sender As Object, ByVal ce As ComponentChangedEventArgs)
            If (ce.Component IsNot Nothing) And (CType(ce.Component, IComponent).Site IsNot Nothing) And (ce.Member IsNot Nothing) Then
                OnUserChange(("The " + ce.Member.Name + " member of the " + CType(ce.Component, IComponent).Site.Name + " component has been changed."))
            End If 
        End Sub 

        ' This method handles the OnComponentChanging event to display a notification.  
        Private Sub OnComponentChanging(ByVal sender As Object, ByVal ce As ComponentChangingEventArgs)
            If (ce.Component IsNot Nothing) And (CType(ce.Component, IComponent).Site IsNot Nothing) And (ce.Member IsNot Nothing) Then
                OnUserChange(("The " + ce.Member.Name + " member of the " + CType(ce.Component, IComponent).Site.Name + " component is being changed."))
            End If 
        End Sub 

        ' This method handles the OnComponentAdded event to display a notification.  
        Private Sub OnComponentAdded(ByVal sender As Object, ByVal ce As ComponentEventArgs)
            OnUserChange(("A component, " + ce.Component.Site.Name + ", has been added."))
        End Sub 

        ' This method handles the OnComponentAdding event to display a notification.  
        Private Sub OnComponentAdding(ByVal sender As Object, ByVal ce As ComponentEventArgs)
            OnUserChange(("A component of type " + (CType(ce.Component, Component)).GetType().FullName + " is being added."))
        End Sub 

        ' This method handles the OnComponentRemoved event to display a notification.  
        Private Sub OnComponentRemoved(ByVal sender As Object, ByVal ce As ComponentEventArgs)
            OnUserChange(("A component, " + ce.Component.Site.Name + ", has been removed."))
        End Sub 

        ' This method handles the OnComponentRemoving event to display a notification.  
        Private Sub OnComponentRemoving(ByVal sender As Object, ByVal ce As ComponentEventArgs)
            OnUserChange(("A component, " + ce.Component.Site.Name + ", is being removed."))
        End Sub 

        ' This method handles the OnComponentRename event to display a notification.  
        Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)
            OnUserChange(("A component, " + ce.OldName + ", was renamed to " + ce.NewName + "."))
        End Sub 

        ' This method adds a specified notification message to the control's listbox. 
        Private Sub OnUserChange(ByVal [text] As String)
            listBox1.Items.Add([text])
        End Sub 

        ' Clean up any resources being used. 
        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                ClearChangeNotifications()

                If (components IsNot Nothing) Then
                    components.Dispose()
                End If 
            End If 
            MyBase.Dispose(disposing)
        End Sub 

    End Class 
End Namespace
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

/*  This sample illustrates how to use the IComponentChangeService interface 
    to handle component change events.  The ComponentClass control attaches 
    event handlers when it is sited in a document, and displays a message 
    when notification that a component has been added, removed, or changed
    is received from the IComponentChangeService.

    To run this sample, add the ComponentClass control to a Form and
    add, remove, or change components to see the behavior of the
    component change event handlers. */ 

namespace IComponentChangeServiceExample 
{
    public class ComponentClass : System.Windows.Forms.UserControl 
    {
        private System.ComponentModel.Container components = null;
	private System.Windows.Forms.ListBox listBox1;
	private IComponentChangeService m_changeService;

	public ComponentClass() 
	{
	    InitializeComponent();
	}

	private void InitializeComponent() 
	{
  	    this.listBox1 = new System.Windows.Forms.ListBox();
	    this.SuspendLayout();

 	    // listBox1. 
	    this.listBox1.Location = new System.Drawing.Point(24, 16);
	    this.listBox1.Name = "listBox1";
	    this.listBox1.Size = new System.Drawing.Size(576, 277);
	    this.listBox1.TabIndex = 0;
		   
	    // ComponentClass. 
	    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listBox1});
	    this.Name = "ComponentClass";
	    this.Size = new System.Drawing.Size(624, 320);

            this.ResumeLayout(false);
	}

	// This override allows the control to register event handlers for IComponentChangeService events 
	// at the time the control is sited, which happens only in design mode. 
	public override ISite Site 
	{
	    get 
	    {
		return base.Site;
	    }
	    set 
	    {		
		// Clear any component change event handlers.
		ClearChangeNotifications();		
				
		// Set the new Site value. 
		base.Site = value;

		m_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

		// Register event handlers for component change events.
		RegisterChangeNotifications();			
	    }
	}

	private void ClearChangeNotifications()
	{
	    // The m_changeService value is null when not in design mode,  
	    // as the IComponentChangeService is only available at design time.	
	    m_changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

 	    // Clear our the component change events to prepare for re-siting.				 
	    if (m_changeService != null) 
	    {
	 	m_changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged);
		m_changeService.ComponentChanging -= new ComponentChangingEventHandler(OnComponentChanging);
		m_changeService.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
		m_changeService.ComponentAdding -= new ComponentEventHandler(OnComponentAdding);
		m_changeService.ComponentRemoved -= new ComponentEventHandler(OnComponentRemoved);
		m_changeService.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);
		m_changeService.ComponentRename -= new ComponentRenameEventHandler(OnComponentRename);
	    }
	}

	private void RegisterChangeNotifications()
	{
	    // Register the event handlers for the IComponentChangeService events 
	    if (m_changeService != null) 
	    {
		m_changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged);
		m_changeService.ComponentChanging += new ComponentChangingEventHandler(OnComponentChanging);
		m_changeService.ComponentAdded += new ComponentEventHandler(OnComponentAdded);
		m_changeService.ComponentAdding += new ComponentEventHandler(OnComponentAdding);
		m_changeService.ComponentRemoved += new ComponentEventHandler(OnComponentRemoved);
		m_changeService.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
		m_changeService.ComponentRename += new ComponentRenameEventHandler(OnComponentRename);
	    }
	}

	/* This method handles the OnComponentChanged event to display a notification. */ 
	private void OnComponentChanged(object sender, ComponentChangedEventArgs ce) 
	{
 	    if( ce.Component != null && ((IComponent)ce.Component).Site != null && ce.Member != null ) 
	    OnUserChange("The " + ce.Member.Name + " member of the " + ((IComponent)ce.Component).Site.Name + " component has been changed.");
	}

	/* This method handles the OnComponentChanging event to display a notification. */ 
	private void OnComponentChanging(object sender, ComponentChangingEventArgs ce) 
	{
	    if( ce.Component != null && ((IComponent)ce.Component).Site != null && ce.Member != null ) 
	    OnUserChange("The " + ce.Member.Name + " member of the " + ((IComponent)ce.Component).Site.Name + " component is being changed.");
	}

	/* This method handles the OnComponentAdded event to display a notification. */ 
	private void OnComponentAdded(object sender, ComponentEventArgs ce) 
	{			
	    OnUserChange("A component, " + ce.Component.Site.Name + ", has been added.");
	}

	/* This method handles the OnComponentAdding event to display a notification. */ 		
	private void OnComponentAdding(object sender, ComponentEventArgs ce) 
	{			
	    OnUserChange("A component of type " + ce.Component.GetType().FullName + " is being added.");
	}

	/* This method handles the OnComponentRemoved event to display a notification. */ 
	private void OnComponentRemoved(object sender, ComponentEventArgs ce) 
	{
	    OnUserChange("A component, " + ce.Component.Site.Name + ", has been removed.");
	}

	/* This method handles the OnComponentRemoving event to display a notification. */ 
	private void OnComponentRemoving(object sender, ComponentEventArgs ce) 
	{
	    OnUserChange("A component, " + ce.Component.Site.Name + ", is being removed.");
	}

	/* This method handles the OnComponentRename event to display a notification. */ 
	private void OnComponentRename(object sender, ComponentRenameEventArgs ce) 
	{
	    OnUserChange("A component, " + ce.OldName + ", was renamed to " + ce.NewName +".");
	}

	// This method adds a specified notification message to the control's listbox. 
	private void OnUserChange(string text) 
	{
	    listBox1.Items.Add(text);
	}

	// Clean up any resources being used. 
	protected override void Dispose( bool disposing ) 
	{
	    if( disposing ) 
	    {
		ClearChangeNotifications();
	        
		if(components != null) 
		{
		    components.Dispose();
		}
	    }
	    base.Dispose( disposing );
	}
    }
}
#using <system.dll>
#using <system.windows.forms.dll>
#using <system.drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Windows::Forms;

/*  This sample illustrates how to use the IComponentChangeService interface
    to handle component change events.  The ComponentClass control attaches
    event handlers when it is sited in a document, and displays a message
    when notification that a component has been added, removed, or changed
    is received from the IComponentChangeService.

    To run this sample, add the ComponentClass control to a Form and
    add, remove, or change components to see the behavior of the
    component change event handlers. */ 

public ref class ComponentClass: public UserControl
{
private:
   System::ComponentModel::Container^ components;
   ListBox^ listBox1;
   IComponentChangeService^ m_changeService;
   void InitializeComponent()
   {
      this->listBox1 = gcnew ListBox;
      this->SuspendLayout();

      // listBox1. 
      this->listBox1->Location = System::Drawing::Point( 24, 16 );
      this->listBox1->Name = "listBox1";
      this->listBox1->Size = System::Drawing::Size( 576, 277 );
      this->listBox1->TabIndex = 0;

      // ComponentClass. 
      array<Control^>^myArray = {listBox1};
      this->Controls->AddRange( myArray );
      this->Name = "ComponentClass";
      this->Size = System::Drawing::Size( 624, 320 );
      this->ResumeLayout( false );
   }

   void ClearChangeNotifications()
   {
      // The m_changeService value is 0 when not in design mode, 
      // as the IComponentChangeService is only available at design time.
      m_changeService = dynamic_cast<IComponentChangeService^>(GetService( IComponentChangeService::typeid ));

      // Clear our the component change events to prepare for re-siting. 
      if ( m_changeService != nullptr )
      {
         m_changeService->ComponentChanged -= gcnew ComponentChangedEventHandler( this, &ComponentClass::OnComponentChanged );
         m_changeService->ComponentChanging -= gcnew ComponentChangingEventHandler( this, &ComponentClass::OnComponentChanging );
         m_changeService->ComponentAdded -= gcnew ComponentEventHandler( this, &ComponentClass::OnComponentAdded );
         m_changeService->ComponentAdding -= gcnew ComponentEventHandler( this, &ComponentClass::OnComponentAdding );
         m_changeService->ComponentRemoved -= gcnew ComponentEventHandler( this, &ComponentClass::OnComponentRemoved );
         m_changeService->ComponentRemoving -= gcnew ComponentEventHandler( this, &ComponentClass::OnComponentRemoving );
         m_changeService->ComponentRename -= gcnew ComponentRenameEventHandler( this, &ComponentClass::OnComponentRename );
      }
   }

   void RegisterChangeNotifications()
   {
      // Register the event handlers for the IComponentChangeService events 
      if ( m_changeService != nullptr )
      {
         m_changeService->ComponentChanged += gcnew ComponentChangedEventHandler( this, &ComponentClass::OnComponentChanged );
         m_changeService->ComponentChanging += gcnew ComponentChangingEventHandler( this, &ComponentClass::OnComponentChanging );
         m_changeService->ComponentAdded += gcnew ComponentEventHandler( this, &ComponentClass::OnComponentAdded );
         m_changeService->ComponentAdding += gcnew ComponentEventHandler( this, &ComponentClass::OnComponentAdding );
         m_changeService->ComponentRemoved += gcnew ComponentEventHandler( this, &ComponentClass::OnComponentRemoved );
         m_changeService->ComponentRemoving += gcnew ComponentEventHandler( this, &ComponentClass::OnComponentRemoving );
         m_changeService->ComponentRename += gcnew ComponentRenameEventHandler( this, &ComponentClass::OnComponentRename );
      }
   }

   /* This method handles the OnComponentChanged event to display a notification. */ 
   void OnComponentChanged( Object^ /*sender*/, ComponentChangedEventArgs^ ce )
   {
      if ( ce->Component != nullptr && static_cast<IComponent^>(ce->Component)->Site != nullptr && ce->Member != nullptr )
            OnUserChange( "The " + ce->Member->Name + " member of the " + static_cast<IComponent^>(ce->Component)->Site->Name + " component has been changed." );
   }


   /* This method handles the OnComponentChanging event to display a notification. */ 
   void OnComponentChanging( Object^ /*sender*/, ComponentChangingEventArgs^ ce )
   {
      if ( ce->Component != nullptr && static_cast<IComponent^>(ce->Component)->Site != nullptr && ce->Member != nullptr )
            OnUserChange( "The " + ce->Member->Name + " member of the " + static_cast<IComponent^>(ce->Component)->Site->Name + " component is being changed." );
   }

   /* This method handles the OnComponentAdded event to display a notification. */ 
   void OnComponentAdded( Object^ /*sender*/, ComponentEventArgs^ ce )
   {
      OnUserChange( "A component, " + ce->Component->Site->Name + ", has been added." );
   }

   /* This method handles the OnComponentAdding event to display a notification. */ 
   void OnComponentAdding( Object^ /*sender*/, ComponentEventArgs^ ce )
   {
      OnUserChange( "A component of type " + ce->Component->GetType()->FullName + " is being added." );
   }

   /* This method handles the OnComponentRemoved event to display a notification. */ 
   void OnComponentRemoved( Object^ /*sender*/, ComponentEventArgs^ ce )
   {
      OnUserChange( "A component, " + ce->Component->Site->Name + ", has been removed." );
   }

   /* This method handles the OnComponentRemoving event to display a notification. */ 
   void OnComponentRemoving( Object^ /*sender*/, ComponentEventArgs^ ce )
   {
      OnUserChange( "A component, " + ce->Component->Site->Name + ", is being removed." );
   }

   /* This method handles the OnComponentRename event to display a notification. */ 
   void OnComponentRename( Object^ /*sender*/, ComponentRenameEventArgs^ ce )
   {
      OnUserChange( "A component, " + ce->OldName + ", was renamed to " + ce->NewName + "." );
   }

   // This method adds a specified notification message to the control's listbox. 
   void OnUserChange( String^ text )
   {
      listBox1->Items->Add( text );
   }

public:
   ComponentClass()
   {
      InitializeComponent();
   }

   property ISite^ Site 
   {
      // This override allows the control to register event handlers for IComponentChangeService events 
      // at the time the control is sited, which happens only in design mode. 
      virtual ISite^ get() override
      {
         return Site;
      }

      virtual void set( ISite^ value ) override
      {
         // Clear any component change event handlers.
         ClearChangeNotifications();

         // Set the new Site value.
         Site = value;
         m_changeService = static_cast<IComponentChangeService^>(GetService( IComponentChangeService::typeid ));

         // Register event handlers for component change events.
         RegisterChangeNotifications();
      }
   }

   // Clean up any resources being used. 
public:
   ~ComponentClass()
   {
      ClearChangeNotifications();
      if ( components != nullptr )
      {
         delete components;
      }
   }
};
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.