The goal of this article is to simplify the process of programmatically adding custom controls to Visual Studio .NET control toolbox.
The accompanying sample will be helpful for component developers, who want to deploy their custom controls to their customers in a package. There is also a sample console application, which demonstrates the techniques presented in this article. This console application can be run from the command line with parameters allowing it to be launched during installations.
Currently, the following VS.NET versions are supported:
Basic familiarity with Visual Studio .NET and C# is necessary for this article. Experience on DTE and Reflection is a plus.
There is one class in this code sample, which provides a single method called
RegisterControls. It takes two parameters:
- VS.NET version (using
- Controls to be added (using
DTE stands for Design Time Environment; it is basically the extensibility object model of VS.NET.
DTEVersion is a flag's
enum that specifies VS.NET versions to be used.
public enum DTEVersion
None = 0x0000,
[Description("VisualStudio.DTE.7")] VS2002 = 0x0001,
[Description("VisualStudio.DTE.7.1")] VS2003 = 0x0002,
struct is used to specify which controls will be added.
public string TabName;
public string ControlPath;
public Type Control;
public bool IsToolBoxTab;
struct has three constructors for different purposes:
public VSControl(Type control, string tabName)
The control will be added to specified toolbox tab.
public VSControl(string assemblyPath, string tabName)
All controls in the assembly will be added to the specified toolbox tab.
public VSControl(string tabGroupName)
A new empty tab will be created in the toolbox.
RegisterControls method first determines the version of DTE and gets the instance of a suitable one.
if((dteVersion & DTEVersion.VS2002) > DTEVersion.None)
dte = GetDesignTimeEnvironment(DTEVersion.VS2002, ref alreadyCreated;
if(dte != null)
RegisterControls(dte, alreadyCreated, controls);
GetDesignTimeEnvironment function returns an instance of either VS.NET 2002 or VS.NET 2003. If there is already an instance running, a reference to that instance is returned. If no instances are found, a new instance is created. When a running instance is found, the next step is to determine whether the toolbox is available. When VS.NET is running, it is not possible to add controls to the toolbox.
string progID = GetEnumDescription(dteVersion);
result = (DTE) Marshal.GetActiveObject(progID);
alreadyCreated = true;
result = null;
result = null;
If there is no running instance of Visual Studio .Net, or the existing one is unusable, we create a new one.
if(result == null)
Type type = Type.GetTypeFromProgID(progID);
if(type != null)
result = (DTE) Activator.CreateInstance(type);
Once we have a suitable DTE,
RegisterControls method is called with a reference to the selected DTE. In the
RegisterControls method, we get the reference to the toolbox window, then retrieve the tabs collection.
Window toolbox = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
ToolBoxTabs tabs = ((ToolBox) toolbox.Object).ToolBoxTabs;
Depending on the developer’s selection, either a new tab is added, or an existing tab is used when creating the controls.
tab = GetToolBoxTab(tabs, control.TabName);
if(tab != null && !control.IsToolBoxTab)
After we finish adding controls, we close the VS.NET instance only if we created it.
It is possible to build quickly a console application that uses the code in this article to add controls easily to the toolbox.
static void Main(string args)
string assemblyPath = @ "C:\Controls\SampleControls.dll";
string tabName = "Sample Controls";
VSControl sampleTabGroup = new VSControl(tabName);
VSControl sampleControl1 =
new VSControl(typeof(SampleControl), tabName);
VSControl sampleControl2 = new VSControl(controlPath, tabName);
bool result = DevEnvironment.RegisterControls(
DTEVersion.VS2002 | DTEVersion.VS2003,
Console.WriteLine("Controls added succesfully.");
"Controls couldn't be added. There is no VS.Net installed.");
Console.WriteLine("Press any key to continue...");
It is possible to extend this sample console application to accept parameters from the command prompt, so that a setup project can run it to add specified controls during installation.