| Larkware |
| We get up early so that you don't have to. |
By Mike Gunderloy
Friday, January 03, 2003I spent some time over the past few days working on a setup program for a new Visual Studio .NET component. As part of the setup, we want to install the component to the Visual Studio .NET Toolbox. Well, what a kettle of fish that turned out to be. I got it working, but not without a bunch of grief and experimentation. So, here's a note to (hopefully) save other developers some trouble.
It turns out that the only way to automate the process is to use the Common Environment Object Model, which is wrapped up in the COM-based envdte library. If you dig into this far enough, you'll find the ToolboxItems.Add method. You'll also find that code you write with this method likely doesn't work as a standalone application, even if it worked within VS .NET in debug mode. You'll also find that most of the information on the Web comes from other people having similar problems.
Anyhow, if you hit this situation, here's my tip: the Add method wants to see the full path and filename of the library containing your component. This is true even if your component is stored in the exact same directory as the code you're running to add it. So here's a bit of C# code that works for me:
EnvDTE.DTE env = new EnvDTE.DTE();
// Get the Toolbox window
Window win = env.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
ToolBox tb = (ToolBox)win.Object;
// objects to work with
ToolBoxTabs tbts = tb.ToolBoxTabs;
ToolBoxTab tbt = null;
ToolBoxItem tbi;
tbt = tbts.Add("MyTabName");
// Tab needs to be active to add control
tbt.Activate();
tbi = tbt.ToolBoxItems.Add("NewControlName", @"C:\InstallDir\NewControl.dll", vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);Ultimately, we'll probably ship this code, along with some other Toolbox-management goodies, in a SpiffyWare product. Meanwhile, I hope this note helps someone out.
Mike Gunderloy is the lead developer for Larkware and author of numerous books and articles on programming topics.
The new component, by the way, will ship from SpiffyWare, an outfit that I am writing a bunch of code for these days.
Update 3/19/03: Shawn Van Ness has some additional information on his blog, which you definitely want to read if you're tackling this problem.