Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to add components to toolbox programmatically

49 views
Skip to first unread message

Marc Scheuner

unread,
Apr 8, 2003, 5:40:57 AM4/8/03
to
Folks,

I am currently writing some internal components to be used in our C#
projects. I was wondering if there's any documented way of adding them
to the VS.NET IDE's toolbox.

How can I install components onto another developer's machine? Yes, I
know, I need to copy the assemblies, probably into the GAC, right?

But how can I add these assemblies to a given (newly created) tab on
their toolbox? I'd like to do this for them, in the install, so that
everyone has the same component tabs.

Also, if I have a .CHM describing these components (created by NDoc),
is there any way of automatically merging this help file into the
VS.NET IDE's help system?

Any hints would be most welcome!
Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch

Mike Bryga

unread,
Apr 8, 2003, 1:51:18 PM4/8/03
to
Marc,

I've been struggling with the same thing. I have not
solved this problem completely, so if you do, please let
me know.

There are two aspects to this problem. The first is
installing into VS.NET. There are 3 ways this is done 1)
Macros, 2) Add-Ins, 3) Packages (search for VSIP on
MSDN.Microsoft.com). The second is manipulating the
ToolBoxTabs and ToolBoxItems collections (see these in the
on-line help for sample code).

My next step is to move my test code (which acts like it
works, but nothing is visible) into the Add-In and see if
the code works when the Add-In is loaded (vs just running
it). Also, I don't quite understand how VS.NET decides
which tabs should be made visible based on the type of
object (form, code, etc.) currently displayed - this might
be a factor in why my tabs are not showing (even with view
all selected).

Please let me know what you find.

Ciao, Mike.

Jeff Davis

unread,
Apr 8, 2003, 3:33:16 PM4/8/03
to
This was not straightforward, but I believe I now have a working solution. Here's what I do:
 
1) Create a strong name for the assemblies containing your components using "sn.exe" and assign the strong names in the "AssemblyInfo.cs" file (I'm not sure if this is necessary or not)
 
2) Create a setup project to install the component assembly files into "Program Files\<Company>\<Product>". You don't need to install your assemblies into the GAC, and it seems that most component vendors just install their components into a directory under "Program Files". I used the bundled Visual Studio Installer for creating this setup package and made it part of my solution. I added the "Primary Output from Assembly xxx" for the relevant assembly to the installation package and installed this file in the "Program Files\Company\Product" directory.
 
3) Add a system registry key to your installation package under "HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\AssemblyFolders\" which is the name of your control set, i.e. "My Controls". Set the value of your new registry key to "[TARGETDIR]", i.e. the directory where you're going to install your assemblies. VS.Net will search this registry branch and automatically display components contained in the assemblies in the registered directories when you invoke the "Customize Toolbox..." command in VS.Net.
 
4) Create an "Installer" component for the assembly containing your components that will automatically add toolbox icons for each of your components. This class should reference the VS.Net automation interfaces for adding icons to the toolbox, although there are some bugs you have to work around. Here is the code for my Installer class, and the installation and uninstallation work for me. You will need to add a reference to "envdte", listed on the ".Net" tab of the "Add Reference..." dialog box. Note that this Installer class expects a parameter for the installation path (parameter will be passed in the next step):
 
 

using System;

using System.Collections;

using System.ComponentModel;

using System.Configuration.Install;

using EnvDTE;

using System.Windows.Forms;

using System.IO;

 

namespace MyControls

{

      /// <summary>

      /// Summary description for MyAssemblyInstaller.

      /// </summary>

      [RunInstaller(true), DesignTimeVisible(false), ToolboxItem(false)]

      public class MyAssemblyInstaller : System.Configuration.Install.Installer

      {

            /// <summary>

            /// Required designer variable.

            /// </summary>

            private System.ComponentModel.Container components = null;

 

            public MyAssemblyInstaller()

            {

                  // This call is required by the Designer.

                  InitializeComponent();

            }

 

            public override void Install(IDictionary savedState)

            {

                  base.Install(savedState);

 

                  string installPath = (string)Context.Parameters["InstallPath"];

 

                  // Install toolbox items and tabs for My Controls

                  DTE devEnv = new DTEClass();

 

                  // Make sure the properties window is visible. This gets around a VS.Net bug when installing

                  // toolbox icons

                  devEnv.ExecuteCommand("View.PropertiesWindow", string.Empty);

 

                  // Get the list of toolbox tabs

                  Window win = devEnv.Windows.Item(Constants.vsWindowKindToolbox);

                  ToolBox tb = (ToolBox)win.Object;

                  ToolBoxTabs toolBoxTabs = tb.ToolBoxTabs;

   

                  // Delete the "My Controls" tab if it already exists

                  foreach (ToolBoxTab t in toolBoxTabs)

                  {

                        if (t.Name == "My Controls")

                              t.Delete();

                  }

   

                  // Add the "My Controls" tab

                  ToolBoxTab myToolboxTab = toolBoxTabs.Add("My Controls");

                  myToolboxTab.Activate();

 

                  installPath = Path.Combine(installPath, "MyAssemblyName.dll");

 

                  // Add controls to the "My Controls" tab

                  myToolboxTab.ToolBoxItems.Add("MyComponentName", installPath,

                        vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);         

 

                  devEnv.Quit();

                  devEnv = null;

            }

 

            public override void Uninstall(IDictionary savedState)

            { 

                  if (savedState != null)

                  {

                        base.Uninstall(savedState);

 

                        // Uninstall toolbox items and tabs for My Controls

                        DTE devEnv = new DTEClass();

     

                        // Get the list of toolbox tabs

                        Window win = devEnv.Windows.Item(Constants.vsWindowKindToolbox);

                        ToolBox tb = (ToolBox)win.Object;

                        ToolBoxTabs toolBoxTabs = tb.ToolBoxTabs;

   

                        // Delete the "My Controls" tab if it already exists

                        foreach (ToolBoxTab t in toolBoxTabs)

                        {

                              if (t.Name == "My Controls")

                                    t.Delete();

                        }

 

                        devEnv.Quit();

                        devEnv = null;

                  }

            }

 

...

 

 

3) Modify the Setup project created in step 2 to call the Installer class created in step 4 using a custom action. You should call the assembly containing your Installer class during Install as well as Uninstall.  Make sure to set the "InstallerClass" property of the custom action to "True". Set the "CustomActionData" property to "/InstallPath="[TARGETDIR]\" ". This will cause the setup project to pass the installation directory selected by the user to your Installer class so you can associate the toolbox icons with the correct assemblies.
 
 
That's the basic overview of the process that works for me. Good luck, and let me know how it goes.

 

 
Jeff Davis
 
"Marc Scheuner" <no....@for.me> wrote in message news:85659v44hsjualdke...@4ax.com...
0 new messages