Adding Custom Tab into Toolbox of VS 2003 or 2005

Souce Code
///
<summary>
/// Adding Custom ToolTab into ToolBox of Visual Studio 2003 or 2005
/// </summary>
/// <param name=”sAssemblyPath”>Path of Assembly</param>
/// <param name=”sTabName”>Tab Name</param>
/// <param name=”sControlName”>Control Name</param>
/// <param name=”isWhidbey”>Check whether VS2k3 or VS2k5</param>
/// <returns></returns>
public static bool RegisterControl(string sAssemblyPath,string sTabName,
string sControlName,bool isWhidbey) {
try {
EnvDTE.DTE dte;

if(isWhidbey)
dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject(“VisualStudio.DTE.8.0”);
else
dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject(“VisualStudio.DTE.7.1”);

ToolBox objToolBox;
ToolBoxTabs colTbxTabs;
ToolBoxTab objTab;
ToolBoxItem objTbxItem ;
// Create an object reference to the IDE’s ToolBox object.
objToolBox = (EnvDTE.ToolBox)dte.Windows.Item(Constants.vsWindowKindToolbox).Object;
//.Object;
colTbxTabs = objToolBox.ToolBoxTabs;

/*
* 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.
*/

//ToolBoxWnd.Visible = true;
//objToolBox.DTE.ExecuteCommand(“View.PropertiesWindow”,””);

for(int i=1; i < colTbxTabs.Count ; i++) {
if(colTbxTabs.Item(i).Name.ToString().Trim() == sTabName) {
Console.WriteLine(colTbxTabs.Item(i).Name.ToString());
objTab = colTbxTabs.Item(i);
foreach(ToolBoxItem tbxi in objTab.ToolBoxItems) {
Console.WriteLine(tbxi.ToString());
//Print ToolboxItem.
}
objTab.Delete();
//Delete if it is already exist.
break;
}
}
// Add a new tab to the ToolBox.
objTab = colTbxTabs.Add(sTabName);
objTab.Activate();

///Adding the .Net control to the Toolbox
if(isWhidbey){
objTbxItem=objTab.ToolBoxItems.Add(sControlName,
sAssemblyPath , vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
}
else{
//Must be null if the control is many child controls. (VS2003)
objTbxItem=objTab.ToolBoxItems.Add(
null,
sAssemblyPath , vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
}

Console.WriteLine(objTab.ToolBoxItems.Item(objTab.ToolBoxItems.Count).Collection.Count);
return true;
}
catch(Exception ex) {
Console.WriteLine(“RegisterControl(string) : ” + ex.Message) ;
return false;
}

}


UsageRegisterControl(txtAsmPath.Text, txtTabName.Text,txtControlName.Name,chkIsWhidbey.Checked);

Note:

  1. The Custom ToolBar will be created in the first instance of VS IDE. (It’s better if you open only one VS IDE.)
  2. Form Designer should be shown. (If TextEditor is shown then, this code wont’ execute properly because VS IDE automatically prevent the usage of Toolbox if Text Editor is shown.)

Download SouceCode : http://www.geocities.com/mchlsync/VSTbxMgr.zip

Let me know if you have any problem with this soucecode. Thanks.

Posted in C#

5 thoughts on “Adding Custom Tab into Toolbox of VS 2003 or 2005

  1. Hi everyone,

    I am facing the same problem with WPF controls.
    I created a very simple WPF custom control by creating a Custom Control Library (WPF) project in the Visual Studio 2005. This control is just a wrapper of the button control. I compiled it successfully.
    Then, I created a simple WPF Windows Application project in the Visual Studio 2005. Next, I tried to add my newly created WPF custom control into a tab of the toolbox of the newly created WPF Windows Application project through the “Choose Items …”. After browsing and selecting the appropriate DLL of the newly created WPF custom control, I got the following message:
    “There are no components in ” that can be placed in the toolbox”
    I also tried to add my WPF custom control in the same tab programmatically like others in using the ToolBox.Items.Add method. This famous method has returned a null object instead of the newly created toolbox item object without throwing any exception. Therefore, my custom control did not appear in that tab of the toolbox.

    I was thinking that because all the WPF projects have to work with the Visual Studio 2005 Extensions for .Net Framework 3.0 including WPF, WCF. However, the integration beetween those extensions and the Visual Studio 2005 was’nt working properly as expected.

    Does anyone have some idea about that to help me out.

    Thanks a lot,

    Van

Leave a Reply

Your email address will not be published. Required fields are marked *