次の方法で共有


IComponentChangeService インターフェイス

コンポーネントの追加、変更、削除、または名前変更のイベントのイベント ハンドラを追加および削除するためのインターフェイスを提供すると共に、 ComponentChanged イベントまたは ComponentChanging イベントを発生させるメソッドを提供します。

この型のすべてのメンバの一覧については、IComponentChangeService メンバ を参照してください。

<ComVisible(True)>
Public Interface IComponentChangeService
[C#]
[ComVisible(true)]
public interface IComponentChangeService
[C++]
[ComVisible(true)]
public __gc __interface IComponentChangeService
[JScript]
public
   ComVisible(true)
interface IComponentChangeService

解説

IComponentChangeService は、次のイベントを処理するメソッドを示すために使用できるインターフェイスを提供します。

  • コンポーネントが追加されたときに発生する ComponentAdded
  • コンポーネントがこれから追加されるときに発生する ComponentAdding
  • コンポーネントが変更されたときに発生する ComponentChanged
  • コンポーネントがこれから変更されるときに発生する ComponentChanging
  • コンポーネントが削除されたときに発生する ComponentRemoved
  • コンポーネントがこれから削除されるときに発生する ComponentRemoving
  • コンポーネントの名前が変更されたときに発生する ComponentRename

通常、デザイン環境では、コンポーネントに対する上記のような追加、変更、削除、名前変更の各イベントが発生します。デザイナは、コンポーネントに影響を与えるデザイン時のアクションを元に戻したりやり直したりする機能を DesignerTransaction オブジェクトを使用して提供する場合に、このインターフェイスのメソッドを呼び出します。詳細については、 DesignerTransaction に関するドキュメントを参照してください。通常は、これらの変更通知を処理するのはルート デザイナだけです。

このサービスは、コンポーネントが変更されたときに発生するイベントや、これから変更されるときに発生するイベントを発生させるメソッドも提供します。 PropertyDescriptor やコンポーネントは、コンポーネントが変更されたこと、これから変更されることを示すために、それぞれ OnComponentChanged メソッドと OnComponentChanging メソッドを使用できます。

使用例

[Visual Basic, C#, C++] IComponentChangeService インターフェイスを使用して、デザイン モードでコンポーネントの追加、削除、変更に関する通知を受け取る方法を次の例に示します。

 
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 Not (m_changeService Is 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 Not (m_changeService Is 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 Not (ce.Component Is Nothing) And Not (CType(ce.Component, IComponent).Site Is Nothing) And Not (ce.Member Is 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 Not (ce.Component Is Nothing) And Not (CType(ce.Component, IComponent).Site Is Nothing) And Not (ce.Member Is 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 Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

    End Class
End Namespace

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

[C++] 
#using <mscorlib.dll>
#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 __gc class ComponentClass : public UserControl {
private:

    System::ComponentModel::Container* components;
    ListBox* listBox1;
    IComponentChangeService* m_changeService;

    void InitializeComponent()
    {
        this->listBox1 = new ListBox();
        this->SuspendLayout();

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

        // ComponentClass.
        Control* myArray[] = {listBox1};
        this->Controls->AddRange( myArray );
        this->Name = S"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(__typeof(IComponentChangeService)));

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

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

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

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

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

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

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

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

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

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

public:

    ComponentClass()
    {
        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 ISite* get_Site()
    {
        return UserControl::get_Site();
    }

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

        // Set the new Site value.
        UserControl::set_Site( value );

        m_changeService = static_cast<IComponentChangeService*>(GetService(__typeof(IComponentChangeService)));

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

protected:

    // Clean up any resources being used.
    void Dispose( bool disposing )
    {
        if( disposing )
        {
            ClearChangeNotifications();

            if(components != 0)
            {
                components->Dispose();
            }

            UserControl::Dispose( disposing );
        }
    }
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.ComponentModel.Design

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

IComponentChangeService メンバ | System.ComponentModel.Design 名前空間 | ComponentEventHandler | ComponentChangedEventHandler | ComponentChangingEventHandler | ComponentRenameEventHandler | MemberDescriptor | DesignerTransaction