In this article, we will walk through the process of adding custom web/Windows controls and components to the Visual Studio Toolbox, using code. This article will be helpful for control developers who want to deploy their controls into a single package to clients. The client need not have to do the extra work of referencing the controls from the Visual Studio editor and adding it to the Toolbox. Once this application is run, it automatically adds all the controls to the Toolbox, and the developer can drag and drop the components on to the page and use it.
The executable can be called from an installer program as a custom action also.
This article assumes a basic familiarity with Visual Studio 2003, Windows Forms programming in the .NET Framework using C# or Visual Basic .NET, and some basics into the mechanism of Reflection and DTE concepts.
This piece of code actually uses Reflection to create an instance of Visual Studio .DTE.7.1. In Visual Studio, the
DTE object is the root of the automation model, which other object models often call "Application". "DTE" is an acronym that stands for "Development Tools Environment".
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.7.1");
object obj = System.Activator.CreateInstance(t, true);
dteObject = (EnvDTE.DTE)obj;
Once an object of
DTE is obtained, all methods and properties for the particular type can be accessed, and using these properties and methods, we will be adding the items to the toolbox.
This code block shows us how to get an instance of the window object corresponding to the Visual Studio Toolbox:
ToolBoxWnd = dteObject.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
tlbTabs = ((ToolBox)ToolBoxWnd.Object).ToolBoxTabs;
ToolBoxTabs object provides a set of methods and properties to access the Toolbox and its Items collection. Using the methods, new items can be added or deleted form the collection. The code sample below creates a new tab using the name specified by the user and adds the controls contained inside the DLL file loaded by the user.
tlbTab = tlbTabs.Add("My New Tab");
ToolBoxWnd.Visible = true;
tlbTab.ToolBoxItems.Add("MY New Tab", "C:\\MyControl.dll",
The toolbox window has to be made visible and the current toolbox tab has to be activated before any controls can be loaded to the tab, else the controls will get added to the default tab.
The add method of the
ToolBoxItems takes 3 parameters:
- Name of the tab to which the controls are to be added.
- Path to the component to be added to the toolbox (DLL file).
- The format for the toolbox item, can be a control or a simple text segment.
The demo project includes a UI where the user can choose a DLL file to load and can add or delete items. The project can be modified to strip off all the UI part with just a basic form, and all the code for adding the items can be written in the
Create an executable file with this application and the EXE can be used with an installer program that runs this executable which in turn adds all the controls to the toolbox.
Creating a Setup and Deployment Project
Open Visual Studio and select Setup and Deployment Project from the New Project screen, and select Setup Project, provide it with a location, and open the project. Copy all application files and DLLs to the application folder; dependencies are automatically detected and added.
Click Custom Actions editor from the Solution Explorer window, the left pane will now show four default actions for the installer: Install, Commit, Rollback and Uninstall.
Right click on Install and select Add Custom Action, this opens up a dialog box to add a custom action file, select the executable file "AddToToolBar.exe" and add the file. After adding the file, set the
CustomInstaller property for the added file to "
False". This should invoke the EXE file from the installer.
Build the Setup project and the controls package is ready to be deployed.