I have a control packaged in an assembly that has many other dependencies. I
want to programmatically add the control to a form at Design time.
When I drag my control from the toolbox manually, Visual Studio
automatically adds all of my control's dependent assemblies to the
application's references list.
When I use DesignerHost.CreateComponent, and set the created component's
parent property to my form, I can get the control to be added, but the
necessary references aren't added. So, naturally, when I try to build, or
close and re-open the form, my project has no idea how to re-create the
control.
How do I add the dependent assembly references to my project before adding
my control instance to the form (programatically). Or is there a better way
to have the control added to the form for me (maybe using IToolBoxService or
something?)
Trevor Germain
Trevor Germain - 10 Nov 2004 18:48 GMT
I may not have been able to accurately describe my problem in my original
post, but I have managed to come up with the solution on my own.
FYI:
Here is how to reproduce the "Double Click" behavior of an item in the
toolbox, to allow VS to add the control to the form, including adding any
necessary references to the project, and correctly initializing all designer
components.
IDesignerHost _DesignerHost =
(IDesignerHost)GetService(typeof(IDesignerHost));
using (DesignerTransaction transaction =
_DesignerHost.CreateTransaction("Add Component"))
{
DocumentDesigner designer =
_DesignerHost.GetDesigner(_DesignerHost.RootComponent) as DocumentDesigner;
if (designer != null)
{
IToolboxUser toolbox = designer as IToolboxUser;
if (toolbox != null)
toolbox.ToolPicked(new ToolboxItem(typeof( <whatever
component / control you wish to create> )));
}
transaction.Commit();
}
Trevor
>I have a control packaged in an assembly that has many other dependencies.
>I want to programmatically add the control to a form at Design time.
[quoted text clipped - 15 lines]
>
> Trevor Germain