Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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)

[ComVisibleAttribute(true)] 
public interface IComponentChangeService
/** @attribute ComVisibleAttribute(true) */ 
public interface IComponentChangeService
ComVisibleAttribute(true) 
public interface IComponentChangeService
Not applicable.

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

  • ComponentAdded, raised when a component is added.

  • ComponentAdding, raised when a component is about to be added.

  • ComponentChanged, raised when a component is changed.

  • ComponentChanging, raised when a component is about to be changed.

  • ComponentRemoved, raised when a component is removed.

  • ComponentRemoving, raised when a component is about to be removed.

  • ComponentRename, raised when a component is renamed.

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.

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.

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 );
	}
    }
}

import System.*;
import System.Data.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import 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 class ComponentClass extends System.Windows.Forms.UserControl
{
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.ListBox listBox1;
    private IComponentChangeService m_changeService;

    public ComponentClass()
    {
        InitializeComponent();
    } //ComponentClass

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

        // listBox1.
        this.listBox1.set_Location(new System.Drawing.Point(24,16));
        this.listBox1.set_Name("listBox1");
        this.listBox1.set_Size(new System.Drawing.Size(576, 277));
        this.listBox1.set_TabIndex(0);

        // ComponentClass.
        this.get_Controls().AddRange(new 
            System.Windows.Forms.Control[] {this.listBox1 });
        this.set_Name("ComponentClass");
        this.set_Size(new System.Drawing.Size(624, 320));
        this.ResumeLayout(false);
    } //InitializeComponent

    // 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.
    /** @property
     */
    public ISite get_Site()
    {
        return super.get_Site();
    } //get_Site

    /** @property 
     */
    public void set_Site(ISite value)
    {
        // Clear any component change event handlers.
        ClearChangeNotifications();

        // Set the new Site value.
        super.set_Site(value);
        m_changeService = (IComponentChangeService)
            GetService(IComponentChangeService.class.ToType());
        // Register event handlers for component change events.
        RegisterChangeNotifications();
    } //set_Site

    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(IComponentChangeService.class.ToType());

        // Clear our the component change events to prepare for re-siting.            
        if (m_changeService != null) {
            m_changeService.remove_ComponentChanged(new 
                ComponentChangedEventHandler(OnComponentChanged));
            m_changeService.remove_ComponentChanging(new 
                ComponentChangingEventHandler(OnComponentChanging));
            m_changeService.remove_ComponentAdded(new 
                ComponentEventHandler(OnComponentAdded));
            m_changeService.remove_ComponentAdding(new 
                ComponentEventHandler(OnComponentAdding));
            m_changeService.remove_ComponentRemoved(new 
                ComponentEventHandler(OnComponentRemoved));
            m_changeService.remove_ComponentRemoving(new 
                ComponentEventHandler(OnComponentRemoving));
            m_changeService.remove_ComponentRename(new 
                ComponentRenameEventHandler(OnComponentRename));
        }
    } //ClearChangeNotifications

    private void RegisterChangeNotifications()
    {
        // Register the event handlers for the IComponentChangeService events
        if (m_changeService != null) {
            m_changeService.add_ComponentChanged(new 
                ComponentChangedEventHandler(OnComponentChanged));
            m_changeService.add_ComponentChanging(new 
                ComponentChangingEventHandler(OnComponentChanging));
            m_changeService.add_ComponentAdded(new 
                ComponentEventHandler(OnComponentAdded));
            m_changeService.add_ComponentAdding(new 
                ComponentEventHandler(OnComponentAdding));
            m_changeService.add_ComponentRemoved(new 
                ComponentEventHandler(OnComponentRemoved));
            m_changeService.add_ComponentRemoving(new 
                ComponentEventHandler(OnComponentRemoving));
            m_changeService.add_ComponentRename(new 
                ComponentRenameEventHandler(OnComponentRename));
        }
    } //RegisterChangeNotifications

    /* This method handles the OnComponentChanged event to 
        // display a notification. 
     */

    private void OnComponentChanged(Object sender, ComponentChangedEventArgs ce)
    {
        if (ce.get_Component() != null && 
            ((IComponent)ce.get_Component()).get_Site() != null &&
            ce.get_Member() != null) {
            OnUserChange("The " + ce.get_Member().get_Name() 
                + " member of the " 
                + ((IComponent)ce.get_Component()).get_Site().get_Name() 
                + " component has been changed.");
        }
    } //OnComponentChanged

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

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

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

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

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

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

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

    // Clean up any resources being used.
    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            ClearChangeNotifications();
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose
} //ComponentClass

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.