I've been playing around with getting the Web Connection controls automatically installed onto the toolbox. This is drastically easier in Visual Studio 2005 than it used to be in previous versions of VS.NET. The Visual Studio team has done a pretty good job on making it easy to install content into VS.NET easier. This goes for components as well as add-ins, templates and macros, which is nice. Unfortunately there are a few shortcoming to this approach (more on that later).
There are actually a number of ways to get controls registered. I'm going to cover two easy ways here that are interesting because they can be done entire externally to Visual Studio which is by creating a .VSI content package and using COM Interop to register controls installed in the Controls directory of the Visual Studio User specific directory.
Using a VSI Template
VSI templates allow you to install content using the Visual Studio Content Installer. A VSI file is a zip file that contains both the a descriptive template that tells Visual Studio what to do with the package and the actual content files. For a Toolbox item the template looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>Web Connection\WebConnectionWebControls.dll</FileName>
<DisplayName>Web Connection</DisplayName>
<Description>Web Connection Toolbox controls for use with the Web Control Framework in VS.NET</Description>
<ContentVersion>1.0</ContentVersion>
<FileContentType>Toolbox Control</FileContentType>
</Content>
</VSContent>
Notice that I'm using a FileName here that is in a subdirectory. This is important – the directory name actually turns into the Toolbox tab that is created.
When you create the ZIP file you'll want to add the following files:
WebConnectionWebControls.vscontent
WebConnection\WebConnectionWebControl.dll
If you have multiple control libraries add another <Content> section in your XML file.
Zip these two files, then rename the zip file .vsi and you're done. You can double click the .VSI file or ShellExecute() it and it will run and install the controls into the toolbox. When the VSI runs it copies the assembly into:
My Documents\VisualStudio 2005\Controls
and registers the controls from there.
NOTE: Make sure all instances of VS.NET are shut down. If one is running the install fails.
Using the Tool.InstallComponents Command
Another approach is to install the files directly and then use the Tools.InstallComponents Visual Studion Command. This command can be invoked in a number of ways.
- Through a command line (devenv.exe /command Tools.InstallCommunityControls)
- Through Visual Studio Commands (Commands Window)
- Through COM Interop
The COM Interop route is actually a good one for me because these controls will be registered from a Visual FoxPro application (the Web Connection Setup routine). Here's the code I use to register the controls:
lcVsDir = GetSpecialFolder(5) + "Visual Studio 2005\"
IF !ISDIR(lcVsDir)
RETURN
ENDIF
*** Toolbox Control Registration
lcProjectTemplatePath = lcVsDir + "Controls\"
IF !ISDIR(lcProjectTemplatePath)
MD (lcProjectTemplatePath)
ENDIF
lcProjectTemplatePath = lcProjectTemplatePath + "Web Connection\"
IF !ISDIR(lcProjectTemplatePath)
MD (lcProjectTemplatePath)
ENDIF
loApi.CopyFile("VisualStudio\WebConnectionWebControls\bin\Debug\WebConnectionWebControls.dll",;
lcProjectTemplatePath + "WebConnectionWebControls.dll")
TRY
WAIT WINDOW NOWAIT "Registering Web Connection Controls in Visual Studio..."
loVs = CREATEOBJECT("VisualStudio.DTE.8.0")
loVs.ExecuteCommand("Tools.InstallCommunityControls","")
CATCH
ENDTRY
WAIT CLEAR
This works great and is completely externalized and I can be sure the latest version of the file is copied rather than having to keep rebuilding VSI files.
FWIW the entire VS.NET IDE is acceesible via COM Interop in this fashion. So if I had the time I could look into actually drilling in and using the IDE directly to get the toolbox and add an assembly directly from the location where it's installed. However, because of a bunch of constants that aren't easily accessible from VFP I'm going to leave that for another day <s>.
What out for this gotcha
So far so good. It works. But… (and why does there always have to be a damn butt – aeh, but) both of these installers are kinda flaky. If you install the same VSI twice it will add the controls in the assembly twice. Or 5 times. Same with the InstallCommunityControls. It looks like both actually call InstallCommunityControls which means the controls can easily double install.
I haven't found any documentation on InstallCommunityControls so I'm not sure if there's a way to unregister controls first before reregistering. As the code is above it causes ALL community controls (that is any controls stored in the MyDocuments…\Controls folder) to be refreshed. So if somebody has already installed a set of controls the may get double installed which is pretty lame if you ask me. Hopefully the full interface provides for a way to selectively register library for the toolbox.
BTW, the whole way the toolbox works is a pain in the ass anyway. It would be so much nicer if you could simple drag and drop a control from the BIN directory (or wherever) directly onto the toolbox instead of the horrible Add Control To Toolbox window that takes for ever to run.
Oh well, still this is better than nothing. In my scenario with this FoxPro interaction I don't expect much of a problem with duplicate control registration.
Other Posts you might also like