Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 32,238 times

Contents

Downloads

Related Categories

Hosting Windows Forms Designers - Making it Work

divil

Making it Work

That's about it for the DesignerHost and its related classes. The way we've written it, all it needs to be instantiated is a ServiceContainer instance. The framework provides a simple implementation of this, so we'll just use that. We want a very simple design surface so we'll just make a form with one side reserved for a propertygrid.

Now, how do we actually put our DesignerHost to use? It comes back to the Root Designer we discussed earlier. The key method is has is GetView, which returns a Control that can be added to any other form or control. This control is what you see when you look at a form in design view; it has a white background, and shows the form in design mode sitting in the top left.

Once we have created our DesignerHost, we (the host application) needs to subscribe to the SelectionChanged event of the ISelectionService interface that we already implemented. This is so that we can display the properties of the selected objects in our propertygrid.

We will use the following code to instantiate a form, put it in to design mode by adding it to our host, get its design view and add that view to the form:

private void Initialize()
{
    IDesignerHost host;
    Form form;
    IRootDesigner rootDesigner;
    Control view;
    // Initialise service container and designer host
    serviceContainer = new ServiceContainer();
    serviceContainer.AddService(typeof(INameCreationService),
    new NameCreationService());
    serviceContainer.AddService(typeof(IUIService), new UIService(this));
    host = new DesignerHost(serviceContainer);
    // Add toolbox service
    serviceContainer.AddService(typeof(IToolboxService), lstToolbox);
    lstToolbox.designPanel = pnlViewHost;
    PopulateToolbox(lstToolbox);
    // Add menu command service
    menuService = new MenuCommandService();
    serviceContainer.AddService(typeof(IMenuCommandService), menuService);
    // Start the designer host off with a Form to design
    form = (Form)host.CreateComponent(typeof(Form));
    form.TopLevel = false;
    form.Text = "Form1";
    // Get the root designer for the form and add its design view to this form
    rootDesigner = (IRootDesigner)host.GetDesigner(form);
    view = (Control)rootDesigner.GetView(ViewTechnology.WindowsForms);
    view.Dock = DockStyle.Fill;
    pnlViewHost.Controls.Add(view);
    // Subscribe to the selectionchanged event and activate the designer
    ISelectionService s = (ISelectionService)serviceContainer.GetService(
    typeof(ISelectionService));
    s.SelectionChanged += new EventHandler(OnSelectionChanged);
    host.Activate();
}

Conclusion

Reading over the code should fill in any gaps that I missed out while writing this. Hopefully this article will encourage more people to make use of the designer architecture in their own applications.

I have provided an example project that covers everything in this article (just click the download link above). Thanks to Ken Swinney for the VB version.

Comments

  • Re: [4351] Hosting Windows Forms Designers

    Posted by timker on 20 Jul 2007

    I have (am still) modifying this code to create a stand-alone Xaml generator for forms. The Xaml is structured for direct consumption by Windows Presentation Foundation. This was a great starting p...

  • Re: Hosting Windows Forms Designers - PocketPC

    Posted by vitek on 17 Jan 2007

    hallo,
    how to change Designer for design PocketPC components?

    htx

    v!tek

  • Re: [4351] Hosting Windows Forms Designers

    Posted by jharry on 30 Oct 2006

    Hi Tim, great article. I am currently creating a designer in a similar manner to your article, used to create pdf files. I am using it to add only textboxes and pictures, and can save the data to&n...

  • Control coordinates

    Posted by hugobq on 19 Sep 2006

    Tim:

    Thanks for your nice article.

    How can I get the location of a control in the design surface. I want to show the coordinates when the user moves the control, but I can't figure how...

  • Re: Remove controls from te form

    Posted by NewbieDude on 11 Jul 2006

    Did you ever figure out how to remove controls from the form?  I need to do that same now