Designer Hosting


There have been a lot of improvements for designer hosting in Whidbey. The task of creating a host form that hosts other designers in Everett is a huge task requiring probably 100s of lines of code. In Whidbey it is down to <10 lines. More on this further down.
But first - What exactly is designer hosting? Simply put it is hosting designers. To understand how hosting works it is important to understand what a designer is in .Net Framework. Simply stated, a designer in the .NET Framework is an object that only exists at design time, and connects to an object that normally exists at runtime. Consider a Button on a Form. At runtime these two objects are associated with each other only by a parent-child relationshipt. At design time the picture is a little more complex. At design time each of them has a designer. So we now have Form, Form designer, Button and Button designer. They are also connected to a host container. The host container has a lot of responsibilities. It provides various services needed by these objects eg. Selection Service to select these objects, Component change Service, etc. It is also responsible for loading designers from some sort of persistent state, and saving them back to that state.

On a high level, we have 3 parts to host designers: Host container, Designer Loader and Serializers.

Host container is the object that implements IDesignerHost - the most important interface for designer hosting. IDesignerHost provides some organization to the random array of designers that components provide. It also holds a reference to DesignerLoader. DesignerLoader is the piece that is responsible for loading the designers and saving the state back. To do this it uses Serializers.

In Whidbey we have a class called DesignSurface that encapsulates all this. To demonstrate how simple it should be for a third party to host a designer, the following sample code will create a Windows Forms designer and display it:

DesignSurface ds = new DesignSurface();

ds.BeginLoad(typeof(Form)); // loads the Form

IDesignerHost idh = ds.GetService(typeof(IDesignerHost)) as IDesignerHost;

Control view = ds.View as Control; // Gets the View of the DesignSurface

Form f = new Form();

f.Controls.Add(view);

view.Dock = DockStyle.Fill;

f.Show(); // Show the View on a Form


Comments (6)

  1. Steve Whitley says:

    I read your article on this in msdn mag, and I had a question (great read by the way).

    I’m wanting to create my own design surface to be hosted in visual studio.  your article covered how to host it in a seperate app, but would I follow the same route to get it into vs?

  2. C#Geek says:

    I read your article on this in msdn mag, and I had a question.  How can I toggle between Designer Host and a Run Time?  I would like to use your HostControl as Designer and also as entry form.  Is this possible?

  3. idos says:

    I have just read your article in msdn mag. And I tried the program. I derived a class MyControl from UserControl class and add it to the toolbox, but when I want to drag it to the form or double clik it in the toolbox, an error appeared. it said Constructor on type MyControl not found…

    what should I do?

    also can you explain more about how to enable drag and drop?

Skip to main content