Click here to Skip to main content
Click here to Skip to main content

Deploying Controls to VS.NET ToolBox Programatically

By , 1 Oct 2004
 

Sample Image - AddItemToToolBar.jpg

Introduction

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.

Background

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.

The Code

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".

//EnvDTE class cannot be instantiated using the new keyword
//instead use Reflection to get an instance of the DTE Object.
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:

//Gets the window Object corrsponding to the visual Studio Toolbox

ToolBoxWnd = dteObject.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
tlbTabs = ((ToolBox)ToolBoxWnd.Object).ToolBoxTabs;

The 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;
ToolBoxWnd.DTE.ExecuteCommand("View.PropertiesWindow","");
tlbTab.Activate();
tlbTab.ToolBoxItems.Item(1).Select();
tlbTab.ToolBoxItems.Add("MY New Tab", "C:\\MyControl.dll", 
    vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);

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:

  1. Name of the tab to which the controls are to be added.
  2. Path to the component to be added to the toolbox (DLL file).
  3. 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 form_load section.

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.

Setup Screen

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.

Setup ScreenShot

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.

Setup Screenshot

Build the Setup project and the controls package is ready to be deployed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kannan.V
Web Developer
United States United States
Member
Kannan has 4+ years of experience in software development, he started his career in VB and asp application development and as new technologies evolved, he felt the need to make use of the advanced functionalities, develop complex applications and take up challenging tasks, it was then when he started falling in love with the DotNet architecture and concepts which really enthralled him do do more projects in dotnet and get to use the full potential of DotNet. He specialises in development of Custom controls and components and tools. He is a Microsoft Certified Applications Developer working with a Fortune 500 Giant in Bangalore, India
 
Kannan is a fun loving person, likes to wander around in the hills, sometimes he gets glued to the computer doing some serious stuff or trying to get something to work. More information about him is available on his site.
 
Visit my Blog at http://kannanv.blogspot.com
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOnly Tabs get added but not the controlsmemberpriya122119 Jan '10 - 22:40 
Hi,
 
I tried the code and I am able to add only the tab to the VS 2008 toolbox.I tried out with the user control dll which contains single control as well as with the one wihich contains many controls.In both the cases it didn't work.
 
Please help!!
QuestionProgrammatically to load wpf custom controls to vs2008 toolbox in windows applicationmembersivat51818 Aug '09 - 0:23 
Hi All,
 
I have created a simple wpf Custom control and i am trying to add it to vs2008 toolbox so that it can be drag and dropped as normal standard controls.
But i am not able to add it.
Code to add the custom controls to toolbox is in windows application using VB.
this is the method which i have did.
 
toolboxtabs.Item("WPFCustomControls").ToolBoxItems.Add("TextBox", "WpfCustomControlLibrary.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent)
 
Can anyone help me out i am in great urgency Confused | :confused:
 
Thanks in Advance
QuestionHow to Load WPF Custom controls programatically to the toolBox?memberBharathGandhi16 Feb '09 - 17:39 
Using the code given I was able to load Win form controls. But not able to load WPF controls.
Please give some inputs to load WPF controls
 

 
Thanks and Regards,
Bharath
AnswerRe: How to Load WPF Custom controls programatically to the toolBox?memberKannan.V17 Feb '09 - 8:39 
Hi Bharath,
 
Actually havent had the time to look into th WPF side of things, will try my best to see if i can help you Smile | :)
QuestionChange 'Banner Text' of 'Welcome' User Interface Dialog ?memberAniket Salunkhe11 Jan '07 - 22:35 
How to change the 'Banner Text' of 'Welcome' User Interface Dialog while creating Set & Deployment in .Net ?
 
I want a Dialog same as 'Welcome' User Interface Dialog but different 'Banner Text'.
 
Best Regards,
Aniket
AnswerRe: Change 'Banner Text' of 'Welcome' User Interface Dialog ?memberKannan.V12 Jan '07 - 9:03 
Hi Aniket,
 
I havent dug into changing the message, but on first look i was not able to change it, so the roundabout i used was to create a banner bitmap with the text i wanted and then use this as the banner bitmap image for the installer.
GeneralRe: Change 'Banner Text' of 'Welcome' User Interface Dialog ?memberAniket Salunkhe13 Jan '07 - 6:27 
Hi,
 

Kannan.V wrote:
create a banner bitmap with the text i wanted and then use this as the banner bitmap image for the installer.

 
I tried same think. But there is default 'Banner Text' in 'Welcom' User Interface! How to remove it (instead of changing it)?
 

Best Regards,
Aniket

GeneralIt works in VS 2003 if you do this...memberAndrew MacDougall16 Mar '06 - 12:52 
Download the sourcecode from:
   http://www.codeproject.com/csharp/addcontrol2toolbox.asp[^]
 
At the end of btn_AddClick add the following:
				ToolBoxWnd.DTE.Quit();
				
				//Do it again the other way.
				
				VSControl _AGDControlsTabGroup = new VSControl(txtTabName.Text);
				VSControl _AGDControls = new VSControl(txtComponentPath.Text, txtTabName.Text);
				
				bool result = DevEnvironment.RegisterControls(DTEVersion.VS2003, _AGDControlsTabGroup, _AGDControls);
 
				if(result)
					MessageBox.Show("Controls added succesfully to tab.");
				else
					MessageBox.Show("Controls couldn't be added.");

QuestionMultiple Controls in DLL - multiple tabs?memberBryan Cairns9 Jan '06 - 16:01 
I am developling a DLL that just has a lot of controls I commonly use in it, about 60 controls so far.
 
Is there a way to make multiple tabs and put only the controls I want in each tab, for example:
 
(tab) My ListBoxes
(item) My Listbox 1
(item) My Listbox 2
(item) My Listbox 3
 
(tab) My ComboBoxes
(item) My Combobox 1
(item) My Combobox 2
(item) My Combobox 3
 
Or would I have to split the controls into seperate DLL files to get them on different tabs?
 
Great article by the way
AnswerRe: Multiple Controls in DLL - multiple tabs?memberKannan.V10 Jan '06 - 10:20 
Hi Bryan,
 
Thanks a lot for your comments,
You have to create seperate Dlls to load the controls into the respective the tabs, its something similar to the drag and drop dll or like adding a tool box item to the toolbox tab, all available controls in the dll get loaded into the active tab.
 
In my opinion, its always better to seperate the controls based on their functionality.
 
Hope this has helped you,
 
Thanks
Kannan.
AnswerRe: Multiple Controls in DLL - multiple tabs?memberMember 237901623 Feb '09 - 13:38 
The MSDN Library implies this is possible by supplying the fully-qualified GAC name of the component rather than the DLL name. So instead of supplying the DLL pathname, type in something like this: WindowControlLibrary1.UserControl1, WindowControlLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<Your Token>. (Replace <Your Token> with your public key token.)
 
See ToolBoxItems.Add Method (EnvDTE)[^]
 
I haven't tried this myself.
GeneralTab get added to the toolbox but not the controls!memberPadmavathy Thiruvenkadam10 Nov '05 - 0:10 
Hi,
 
I'm able to add the tab to the toolbox , but failed to get the controls loaded to it, evenafter opening new windows application after executing my application.Frown | :(
 
Help!!!!!!!!!!!
 

 
With regards,Padma
GeneralRe: Tab get added to the toolbox but not the controls!memberKannan.V10 Nov '05 - 12:52 
As per the next message you have sent i hope you must have succedded in adding the controls to the toolbox.
 
Regarding your query as below, i believe there is no way to load only a set of controls from a Dll file through code as of i know, may be i might not know how to do it.
 
----------------------------------------------
Hi,

If i give a dll containing more than 1 control, all the controls in that dll get loaded to the

toolbox'tab.Please Tell me How to add a particular control in that dll to a toolbox's Tab?



With regards,Padma
--------------------------------------------------
GeneralA little help, pleasemembermatheussi5 Sep '05 - 3:57 
Hello. First, congratulations! The article is great!
 
Im trying to use your example, but the ide (visual studio .net 2003) send me the exception message: The type or namespace name 'EnvDTE' could not be found (are you missing a using directive or an assembly reference?)
 
Please, what's wrong?
 
Thank's!
Denis
 
Denis Matheussi
GeneralRe: A little help, pleasememberKannan.V5 Sep '05 - 22:09 
Hi Denis,
 
Thanks for the comments,
 
I guess i have missed that line in my article,
you need to add a reference to the EnvDte, right click on your project references node, from the .net tab select envdte and add the reference to your project.
 
Once done, You should all be set, do get back for any concerns/questions.
 
regards
Kannan
GeneralRe: A little help, pleasemembermatheussi6 Sep '05 - 2:54 
Thanks, Kannan!
 
It's working.
 
regards
Denis Matheussi
 

 
Denis Matheussi
Brazil
General[Msg Deleted]membermatheussi6 Sep '05 - 3:02 

GeneralTab not visible for all Projectssussrsarun10 May '05 - 0:19 
Hi,
 
I have a problem while trying out this sample. I add a component using the existing DTE object instead of creating a new DTE object. The component is visble in the current Solution. But when i open another solution it is not visible.
 
If i create a new DTE object and add a component, the tab itself is not visible in the solution. In other solutions, the tab is visble but the Component is not visible.
 
Can anyone help me?
 
Thanks.
rsarun
GeneralRe: Tab not visible for all ProjectsmemberKannan.V10 May '05 - 6:23 
hi,
 
What kind of an object are you trying to add, basically a web control might not be available or shown on the tab when in a windows form.
 
If u have added a web control to the tab , try opening a web project and check if u get to see the control on the tab.
 
Hope it helps
Kannan.V
GeneralRe: Tab not visible for all Projectssussrsarun10 May '05 - 21:43 
Thanks dor the reply.
 
But this does not solve my problem. I am adding a windows control and opening a window form, yet only the tab is visble anf the control is not visible.
 
However, i found another site where i got another code segment.

http://weblogs.asp.net/savanness/archive/2003/03/18/4019.aspx
 
Basically both the codes seem to be the same. But i am able to add the control using the code given in the above site.
 

 
Thanks.
With Regards,
Arun
GeneralRe: Tab not visible for all Projectssussrsarun11 May '05 - 3:14 
Hi Kannan,
 
Sorry, i didnt mention that i am working in VS.Net 2005. So, i have changed the version of the "VisualStudio.DTE.8.0" instead of the original "7.1" version.
 
Also, the code in the blog i mentioned works only for VS.Net 2003. It also does not work in VS.Net 2005. Both code adds the tab but displaya a mesage "There are no usable controls in this group. Drag an item onto this text to add it to Toolbox."
 
I am using a simple windows control developed using VS.Net 2005. Is there anything else other than the version that need to be changed for the Whidbey version.

 
Thanks in Advance.
Arun
QuestionWhy &quot;View.PropertiesWindow&quot; ?memberajproch13 Apr '05 - 5:19 
What is the purpose of the two bolded lines below? The code seems to work fine without them. Thanks!
 
tlbTab = tlbTabs.Add("My New Tab");
ToolBoxWnd.Visible = true;
ToolBoxWnd.DTE.ExecuteCommand("View.PropertiesWindow","");
tlbTab.Activate();
tlbTab.ToolBoxItems.Item(1).Select();
tlbTab.ToolBoxItems.Add("MY New Tab", "C:\\MyControl.dll",
vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
AnswerRe: Why &quot;View.PropertiesWindow&quot; ?memberKannan.V13 Apr '05 - 6:52 
hi,
 
In some cases if the propeties window is hidded or closed, this line will activate the properties window.
And the second line you have cited will add the controls to the active tab.
In some case if the tab is not made active, the controls will be added either to the active tab or to the general tab section.
 
It might work in your case, but does not in certain cases, better to do it sequentially giving no room for errors.
 
regds
Kannan.V
GeneralRe: Why &quot;View.PropertiesWindow&quot; ?memberajproch13 Apr '05 - 10:21 
Thanks for your quick reply.
 
I guess I don't understand why the *Properties* window has to be activated before adding an item to the Toolbox tab -- How does that help to ensure that the Toolbox tab is activated? Is this a workaround for a quirk in VS? Or did you mean that the *Toolbox* window should be visible & activated?
 
As for the second line, isn't the tlbTab.Activate() line (which precedes it) sufficient to activate the new tab? Why does the first item (which I assume is the "Pointer" item added by default) also need to be selected before adding the control?
 
Thanks again.
Generalthink you have a bugmemberJCollum29 Dec '04 - 13:57 
I tried using this with a couple of different controls (one of my own and an Infragistics control) and of the 5 times that I tried I never saw the toolbox tab. Even after restarting the IDE and making sure all the devenv's had been killed. Confused | :confused:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 2 Oct 2004
Article Copyright 2004 by Kannan.V
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid