IDesignerHost Interface

Definition

Provides an interface for managing designer transactions and components.

C#
public interface IDesignerHost : IServiceProvider, System.ComponentModel.Design.IServiceContainer
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IDesignerHost : IServiceProvider, System.ComponentModel.Design.IServiceContainer
C#
public interface IDesignerHost : System.ComponentModel.Design.IServiceContainer
Derived
Attributes
Implements

Examples

The following example code demonstrates how to obtain the IDesignerHost service interface from a designer or sited component.

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

The following example code demonstrates using the IDesignerHost interface to list project components.

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()
        {
            using (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.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 );
        }	
    }
}

Remarks

IDesignerHost is an interface that works with the .NET Framework forms designer architecture to provide support for designer transaction and component management.

The .NET Framework does not provide an implementation of this interface. The interface is implemented by development tools that support designers.

Notes to Callers

To obtain an implementation of IDesignerHost from a development environment, call GetService(Type) while your component is active in design mode, passing the type of IDesignerHost to request an IDesignerHost service interface.

IDesignerHost provides the following members related to designer state:

  • The Loading property indicates whether a designer or document is being loaded.

  • The Activated event occurs when a designer is activated before display.

  • The Deactivated event occurs when a designer is deactivated.

  • The LoadComplete event occurs after a document is loaded.

  • The Activate() method activates the designer.

IDesignerHost provides the following members related to managing components:

IDesignerHost provides the following members related to managing transactions:

Properties

Container

Gets the container for this designer host.

InTransaction

Gets a value indicating whether the designer host is currently in a transaction.

Loading

Gets a value indicating whether the designer host is currently loading the document.

RootComponent

Gets the instance of the base class used as the root component for the current design.

RootComponentClassName

Gets the fully qualified name of the class being designed.

TransactionDescription

Gets the description of the current transaction.

Methods

Activate()

Activates the designer that this host is hosting.

AddService(Type, Object, Boolean)

Adds the specified service to the service container, and optionally promotes the service to any parent service containers.

(Inherited from IServiceContainer)
AddService(Type, Object)

Adds the specified service to the service container.

(Inherited from IServiceContainer)
AddService(Type, ServiceCreatorCallback, Boolean)

Adds the specified service to the service container, and optionally promotes the service to parent service containers.

(Inherited from IServiceContainer)
AddService(Type, ServiceCreatorCallback)

Adds the specified service to the service container.

(Inherited from IServiceContainer)
CreateComponent(Type, String)

Creates a component of the specified type and name, and adds it to the design document.

CreateComponent(Type)

Creates a component of the specified type and adds it to the design document.

CreateTransaction()

Creates a DesignerTransaction that can encapsulate event sequences to improve performance and enable undo and redo support functionality.

CreateTransaction(String)

Creates a DesignerTransaction that can encapsulate event sequences to improve performance and enable undo and redo support functionality, using the specified transaction description.

DestroyComponent(IComponent)

Destroys the specified component and removes it from the designer container.

GetDesigner(IComponent)

Gets the designer instance that contains the specified component.

GetService(Type)

Gets the service object of the specified type.

(Inherited from IServiceProvider)
GetType(String)

Gets an instance of the specified, fully qualified type name.

RemoveService(Type, Boolean)

Removes the specified service type from the service container, and optionally promotes the service to parent service containers.

(Inherited from IServiceContainer)
RemoveService(Type)

Removes the specified service type from the service container.

(Inherited from IServiceContainer)

Events

Activated

Occurs when this designer is activated.

Deactivated

Occurs when this designer is deactivated.

LoadComplete

Occurs when this designer completes loading its document.

TransactionClosed

Adds an event handler for the TransactionClosed event.

TransactionClosing

Adds an event handler for the TransactionClosing event.

TransactionOpened

Adds an event handler for the TransactionOpened event.

TransactionOpening

Adds an event handler for the TransactionOpening event.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also