次の方法で共有


IDesignerHost インターフェイス

デザイナのトランザクションおよびコンポーネントを管理するためのインターフェイスを提供します。

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

<ComVisible(True)>
Public Interface IDesignerHost   Inherits IServiceContainer, IServiceProvider
[C#]
[ComVisible(true)]
public interface IDesignerHost : IServiceContainer,   IServiceProvider
[C++]
[ComVisible(true)]
public __gc __interface IDesignerHost : public IServiceContainer,   IServiceProvider
[JScript]
public
   ComVisible(true)
interface IDesignerHost implements IServiceContainer,   IServiceProvider

解説

IDesignerHost は、.NET Framework のフォーム デザイナ アーキテクチャと連動し、デザイナのトランザクションおよびコンポーネントを管理できるようにするインターフェイスです。

.NET Framework は、このインターフェイスの実装を提供しません。このインターフェイスは、デザイナをサポートしている開発ツールによって実装されます。

呼び出し時の注意: 開発環境から IDesignerHost の実装を取得するには、コンポーネントがデザイン モードでアクティブな間に GetService を呼び出します。これを行うには、 IDesignerHost の型を渡して、 IDesignerHost サービス インターフェイスを要求します。

IDesignerHost は、デザイナの状態に関連する次のメンバを提供します。

  • Loading プロパティは、デザイナまたはドキュメントが読み込まれているかどうかを示します。
  • Activated イベントは、デザイナが表示される前にアクティブになったときに発生します。
  • Deactivated イベントは、デザイナがアクティブでなくなったときに発生します。
  • LoadComplete イベントは、ドキュメントが読み込まれた後に発生します。
  • Activate メソッドは、デザイナをアクティブにします。

IDesignerHost は、コンポーネントの管理に関連する次のメンバを提供します。

  • Container プロパティは、デザイナ ホストのコンテナを示します。
  • RootComponent プロパティは、ルート コンポーネントの基本クラスを示します。
  • RootComponentClassName プロパティは、ルート コンポーネントのクラスの名前を示します。
  • CreateComponent メソッドは、指定した型のコンポーネントを作成します。
  • DestroyComponent メソッドは、指定したコンポーネントを破棄します。
  • GetDesigner メソッドは、指定したコンポーネントに関連付けられているデザイナを取得します。
  • GetType メソッドは、指定した名前の型のインスタンスを取得します。

IDesignerHost は、トランザクションの管理に関連する次のメンバを提供します。

  • InTransaction プロパティは、デザイナでトランザクションが実行中かどうかを示します。
  • TransactionDescription プロパティは、現在のトランザクションの説明を示します。
  • TransactionClosed イベントは、トランザクションが完了すると発生します。
  • TransactionClosing イベントは、トランザクションが完了する直前に発生します。
  • TransactionOpened イベントは、トランザクションが開始されると発生します。
  • TransactionOpening イベントは、トランザクションが開始される直前に発生します。
  • CreateTransaction メソッドは、新しいトランザクションを作成して返します。

使用例

[Visual Basic, C#, C++] デザイナまたは配置されたコンポーネントから、 IDesignerHost サービス インターフェイスを取得する方法については、次のコード例を参照してください。

 
' Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

[C#] 
// Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
IDesignerHost dh = (IDesignerHost) this.Component.Site.GetService(typeof(IDesignerHost));            

[C++] 
// Requests an IDesignerHost service from the design time environment using Component.Site.GetService()
IDesignerHost* dh = static_cast<IDesignerHost*>(this->Component->Site->GetService(__typeof(IDesignerHost)));

[Visual Basic, C#, C++] IDesignerHost インターフェイスを使用して、プロジェクトのコンポーネントの一覧を表示する方法については、次のコード例を参照してください。

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

Namespace IDesignerHostExample
   
   ' IDesignerHostExampleComponent is a component associated 
   ' with the IDesignerHostExampleDesigner that demonstrates 
   ' acquisition and use of the IDesignerHost service 
   ' to list project components.
    <DesignerAttribute(GetType(IDesignerHostExampleDesigner))> _
    Public Class IDesignerHostExampleComponent
        Inherits System.ComponentModel.Component

        Public Sub New()
        End Sub 'New

        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub 
    End Class 

    ' You can double-click the component of a IDesignerHostExampleDesigner
    ' to show a form containing a listbox that lists the name and type 
    ' of each component or control in the current design-time project.
    Public Class IDesignerHostExampleDesigner
        Implements IDesigner
        Private component_ As System.ComponentModel.IComponent

        Public Sub New()
        End Sub 'New

        Public Sub DoDefaultAction() Implements IDesigner.DoDefaultAction
            ListComponents()
        End Sub

        Public Sub Initialize(ByVal component As System.ComponentModel.IComponent) Implements IDesigner.Initialize
            Me.component_ = component
            MessageBox.Show("Double-click the IDesignerHostExample component to view a list of project components.")
        End Sub

        ' Displays a list of components in the current design 
        ' document when the default action of the designer is invoked.
        Private Sub ListComponents()
            Dim listform As New DesignerHostListForm()
            ' Obtain an IDesignerHost service from the design environment.
            Dim host As IDesignerHost = CType(Me.component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
            ' Get the project components container (control containment depends on Controls collections)
            Dim container As IContainer = host.Container
            ' Add each component's type name and name to the list box.
            Dim comp As Component
            For Each comp In container.Components
                listform.listBox1.Items.Add((comp.GetType().Name + " : " + component.Site.Name))
            Next comp
            ' Display the form.
            listform.ShowDialog()
        End Sub 

        Public ReadOnly Property Component() As System.ComponentModel.IComponent Implements IDesigner.Component
            Get
                Return component_
            End Get
        End Property

        Public ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection Implements IDesigner.Verbs
            Get
                Dim dvc As New DesignerVerbCollection()
                dvc.Add(New DesignerVerb("List Components", New EventHandler(AddressOf ListHandler)))
                Return dvc
            End Get
        End Property

        Private Sub ListHandler(ByVal sender As Object, ByVal e As EventArgs)
            ListComponents()
        End Sub 

        Public Sub Dispose() Implements IDisposable.Dispose
        End Sub 
    End Class 
    _ 

    ' Provides a form containing a list box that can display 
    ' a list of project components.
    Public Class DesignerHostListForm
        Inherits System.Windows.Forms.Form
        Public listBox1 As System.Windows.Forms.ListBox
        Private ok_button As System.Windows.Forms.Button

        Public Sub New()
            Me.Name = "DesignerHostListForm"
            Me.Text = "List of design-time project components"
            Me.SuspendLayout()
            Me.listBox1 = New System.Windows.Forms.ListBox()
            Me.listBox1.Location = New System.Drawing.Point(8, 8)
            Me.listBox1.Name = "listBox1"
            Me.listBox1.Size = New System.Drawing.Size(385, 238)
            Me.listBox1.TabIndex = 0
            Me.listBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right)
            Me.ok_button = New System.Windows.Forms.Button()
            Me.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.ok_button.Location = New System.Drawing.Point(232, 256)
            Me.ok_button.Name = "ok_button"
            Me.ok_button.TabIndex = 1
            Me.ok_button.Text = "OK"
            Me.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(400, 285)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ok_button, Me.listBox1})
            Me.ResumeLayout(False)
        End Sub 

        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub 
    End Class 
End Namespace 

[C#] 
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace IDesignerHostExample
{    
    // IDesignerHostExampleComponent is a component associated 
    // with the IDesignerHostExampleDesigner that demonstrates 
    // acquisition and use of the IDesignerHost service 
    // to list project components.
    [DesignerAttribute(typeof(IDesignerHostExampleDesigner))]
    public class IDesignerHostExampleComponent : System.ComponentModel.Component
    {
        public IDesignerHostExampleComponent()
        {}

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }
    }

    // You can double-click the component of an IDesignerHostExampleDesigner 
    // to show a form containing a listbox that lists the name and type 
    // of each component or control in the current design-time project.
    public class IDesignerHostExampleDesigner : IDesigner
    {
        private System.ComponentModel.IComponent component;

        public IDesignerHostExampleDesigner()
        {}

        public void DoDefaultAction()
        {
            ListComponents();
        }

        public void Initialize(System.ComponentModel.IComponent component)
        {
            this.component = component;
            MessageBox.Show("Double-click the IDesignerHostExample component to view a list of project components.");
        }

        // Displays a list of components in the current design 
        // document when the default action of the designer is invoked.
        private void ListComponents()
        {            
            DesignerHostListForm listform = new DesignerHostListForm();
            // Obtain an IDesignerHost service from the design environment.
            IDesignerHost host = (IDesignerHost)this.component.Site.GetService(typeof(IDesignerHost));
            // Get the project components container (control containment depends on Controls collections)
            IContainer container = host.Container;
            // Add each component's type name and name to the list box.
            foreach(IComponent component in container.Components)
            {
                listform.listBox1.Items.Add(component.GetType().Name + " : " + component.Site.Name);
            }
            // Display the form.
            listform.ShowDialog();
        }

        public System.ComponentModel.IComponent Component
        {
            get
            {
                return this.component;
            }
        }

        public System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection dvc = new DesignerVerbCollection();
                dvc.Add( new DesignerVerb("List Components", new EventHandler(ListHandler)) );
                return dvc;
            }
        }

        private void ListHandler(object sender, EventArgs e)
        {
            ListComponents();
        }

        public void Dispose() {    }
    }

    // Provides a form containing a listbox that can display 
    // a list of project components.
    public class DesignerHostListForm : System.Windows.Forms.Form
    {
        public System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button ok_button;
        
        public DesignerHostListForm()
        {
            this.Name = "DesignerHostListForm";
            this.Text = "List of design-time project components";
            this.SuspendLayout();
            this.listBox1 = new System.Windows.Forms.ListBox();                        
            this.listBox1.Location = new System.Drawing.Point(8, 8);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(385, 238);
            this.listBox1.TabIndex = 0;    
            this.listBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);        
            this.ok_button = new System.Windows.Forms.Button();
            this.ok_button.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.ok_button.Location = new System.Drawing.Point(232, 256);
            this.ok_button.Name = "ok_button";
            this.ok_button.TabIndex = 1;
            this.ok_button.Text = "OK";
            this.ok_button.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(400, 285);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.ok_button, this.listBox1 });
            this.ResumeLayout(false);    
        }

        protected override void Dispose( bool disposing )
        {            
            base.Dispose( disposing );
        }    
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace IDesignerHostExample
{    
    // Provides a form containing a listbox that can display 
    // a list of project components.
public __gc class DesignerHostListForm : public System::Windows::Forms::Form
{
public:
    System::Windows::Forms::ListBox* listBox1;
private:
    System::Windows::Forms::Button* ok_button;

public:
    DesignerHostListForm()
    {
        this->Name = S"DesignerHostListForm";
        this->Text = S"List of design-time project components";
        this->SuspendLayout();
        this->listBox1 = new System::Windows::Forms::ListBox();                        
        this->listBox1->Location = System::Drawing::Point(8, 8);
        this->listBox1->Name = S"listBox1";
        this->listBox1->Size = System::Drawing::Size(385, 238);
        this->listBox1->TabIndex = 0;    
        this->listBox1->Anchor = static_cast<AnchorStyles>(((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom) 
            | System::Windows::Forms::AnchorStyles::Left) 
            | System::Windows::Forms::AnchorStyles::Right);        
        this->ok_button = new System::Windows::Forms::Button();
        this->ok_button->DialogResult = System::Windows::Forms::DialogResult::OK;
        this->ok_button->Location = System::Drawing::Point(232, 256);
        this->ok_button->Name = S"ok_button";
        this->ok_button->TabIndex = 1;
        this->ok_button->Text = S"OK";
        this->ok_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right);
        this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
        this->ClientSize = System::Drawing::Size(400, 285);

        System::Windows::Forms::Control* temp2 [] = {this->ok_button, this->listBox1};

        this->Controls->AddRange(temp2);
        this->ResumeLayout(false);    
    }

protected:
    void Dispose( bool disposing )
    {            
        Form::Dispose( disposing );
    }    
};

// You can double-click the component of an IDesignerHostExampleDesigner 
// to show a form containing a listbox that lists the name and type 
// of each component or control in the current design-time project.
public __gc class IDesignerHostExampleDesigner : public IDesigner
{
private:
    System::ComponentModel::IComponent* component;

public:
    IDesignerHostExampleDesigner()
    {}

    void DoDefaultAction()
    {
        ListComponents();
    }

    void Initialize(System::ComponentModel::IComponent* component)
    {
        this->component = component;
        MessageBox::Show(S"Double-click the IDesignerHostExample component to view a list of project components.");
    }

    // Displays a list of components in the current design 
    // document when the default action of the designer is invoked.
private:
    void ListComponents()
    {            
        DesignerHostListForm* listform = new DesignerHostListForm();
        // Obtain an IDesignerHost service from the design environment.
        IDesignerHost* host = dynamic_cast<IDesignerHost*>(this->component->Site->GetService(__typeof(IDesignerHost)));
        // Get the project components container (control containment depends on Controls collections)
        IContainer* container = host->Container;
        // Add each component's type name and name to the list box.
        System::Collections::IEnumerator* myEnum = container->Components->GetEnumerator();
        while (myEnum->MoveNext())
        {
            IComponent* component = __try_cast<IComponent*>(myEnum->Current);
            listform->listBox1->Items->Add(String::Concat( component->GetType()->Name, S" : ", component->Site->Name ));
        }
        // Display the form.
        listform->ShowDialog();
    }

public:
    __property System::ComponentModel::IComponent* get_Component()
    {
        return this->component;
    }

    __property System::ComponentModel::Design::DesignerVerbCollection* get_Verbs()
    {
        DesignerVerbCollection* dvc = new DesignerVerbCollection();
        dvc->Add( new DesignerVerb(S"List Components", new EventHandler(this, &IDesignerHostExampleDesigner::ListHandler)) );
        return dvc;
    }

private:
    void ListHandler(Object* /*sender*/, EventArgs* /*e*/)
    {
        ListComponents();
    }

public:
    void Dispose() {    }
};

// IDesignerHostExampleComponent is a component associated 
// with the IDesignerHostExampleDesigner that demonstrates 
// acquisition and use of the IDesignerHost service 
// to list project components.
[DesignerAttribute(__typeof(IDesignerHostExampleDesigner))]
public __gc class IDesignerHostExampleComponent : public System::ComponentModel::Component
{
public:
    IDesignerHostExampleComponent()
    {}

protected:
    void Dispose( bool disposing )
    {
        Component::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 内)

参照

IDesignerHost メンバ | System.ComponentModel.Design 名前空間 | IDesigner | IDesignerEventService | IDesignerOptionService