Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Programmaticlly add controls to host
 

Programmaticlly add controls to host

I have an application that allows users to design a dashboard and I use an IDesignerHost implementation. Once they design the dashboard I save the specifics to a database. I want to then be able to load the definition from a database and then programmatically add to my IDesignerHost implementation. I am able to do all of the above except programmatically add to my IDesignerHost implementation...nothing is added to the design host. I have tried

toolitem = toolboxService.GetSelectedToolboxItem();
addedControl = toolitem.CreateComponents(designerHost);

but nothing is visibly added and I have also tried

gaugeAdded = designerHost.CreateComponent(typeof(GaugeContainer), designObject.ControlName) as GaugeContainer

and nothing is added. The only thing that works is when I add the components directly to the form that hosts the designer. However, the components are not selectable, but that is to be expected.

Any ideas?

Thanks
danielreber  Monday, April 02, 2007 8:17 PM
This is how I did it:

DesignSurface surface;

public void AddComponent(IComponent component)
{
((Form)surface.ComponentContainer.Components[0]).Controls.Add(component);
}


surface.ComponentContainer.Components[0] will be of whatever type you passed into surface.BeginLoad(Type), and it's the main form that you are designing.

I don't know if you have to use designerHost.CreateComponent() or not, I did and it worked but I haven't tried without yet. I guess it probably would, but you may not get the nice designer stuff, like the sizing/drag handles.

Let me know how it goes for you Smile

Ben

EvilPenguin  Monday, April 02, 2007 11:47 PM
This is how I did it:

DesignSurface surface;

public void AddComponent(IComponent component)
{
((Form)surface.ComponentContainer.Components[0]).Controls.Add(component);
}


surface.ComponentContainer.Components[0] will be of whatever type you passed into surface.BeginLoad(Type), and it's the main form that you are designing.

I don't know if you have to use designerHost.CreateComponent() or not, I did and it worked but I haven't tried without yet. I guess it probably would, but you may not get the nice designer stuff, like the sizing/drag handles.

Let me know how it goes for you Smile

Ben

EvilPenguin  Monday, April 02, 2007 11:47 PM
Dear Evil,

Thanks for the point in the right direction but it looks like I am using the .net 1.1 classes since I am using IDesignerHost instead of DesignSurface. I would rather not convert my code, I will try to use IDesignerHost until I can't figure out how to add a control programmaticlly.

Regards,
danielreber  Tuesday, April 03, 2007 11:11 AM
I just noticed figure out how to do it using your example code and using IDesignerHost by using ((Form)designerHost.Components[0]).Controls.Add(chartAdded); but I am not getting the drag handles or the select ability. Any more suggestions would be great.

Thanks
danielreber  Tuesday, April 03, 2007 11:30 AM
I found an example of implementing DesignerSurface at http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/ and I downloaded the source code. I then added the below code and the button is added but I am not able to select it or view it's properties, am I missing something?

Button newButton = new Button();
newButton.Top = 100;
newButton.Left = 100;
newButton.Height = 100;
newButton.Width = 100;
newButton.Text = "New";
newButton.Visible = true;

((Form)_hostSurfaceManager.ActiveDesignSurface.ComponentContainer.Components[0]).Controls.Add(newButton);

Thanks
danielreber  Tuesday, April 03, 2007 11:51 AM
I believe that I have figured it out. The below code works

chartAdded = designerHost.CreateComponent(typeof(Chart), designObject.ControlName) as Chart;
chartAdded.Top = top;
chartAdded.Left = left;
chartAdded.Height = height;
chartAdded.Width = width;
((Form)designerHost.Components[0]).Controls.Add(chartAdded);

Ben, I guess that is what you meant by

'I don't know if you have to use designerHost.CreateComponent() or not, I did and it worked but I haven't tried without yet. I guess it probably would, but you may not get the nice designer stuff, like the sizing/drag handles.'

Thanks
  • Proposed As Answer byClement H. Tuesday, September 08, 2009 9:21 AM
  •  
danielreber  Tuesday, April 03, 2007 1:17 PM
Thank you for posting what you found, you just save me from few hours of technical reading all over the web (l).
Clement H.  Tuesday, September 08, 2009 9:23 AM

You can use google to search for other answers

Custom Search

More Threads

• Adding EDIT function to DATAGRID Rows
• How to close form instantly?
• ListViewItem
• How to popup context menu on a UserControl
• The type or namespace name IDump,TextProperty,RemoteFilePathProperty,SourceFileproperty,TransferPorotocolProperty,ValidityCheck,DestinationFileProperty could not be found (are you missing a using directive or an assembly
• Components, Design Time and Remoting issue
• If designer with new control crashes -what to do?
• Corrupt Form - Designer cannot open : help
• Problem with Usercontrol
• Overriding DataGridTextBoxColumn