Click here to Skip to main content
6,595,444 members and growing! (20,086 online)
Email Password   helpLost your password?
General Programming » Macros and Add-ins » VS.NET Addins     Intermediate

Adding Custom Controls to Visual Studio.Net Toolbox Programatically

By Serkan Inci

The goal of this article is to simplify the process of programmatically adding custom controls to Visual Studio .Net control toolbox.
C#, Windows, .NET 1.1, Visual Studio, Dev
Posted:13 Jan 2005
Views:63,779
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.52 Rating: 4.19 out of 5

1
1 vote, 8.3%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
9 votes, 75.0%
5

Introduction

The goal of this article is to simplify the process of programmatically adding custom controls to Visual Studio .NET control toolbox.

The accompanying sample will be helpful for component developers, who want to deploy their custom controls to their customers in a package. There is also a sample console application, which demonstrates the techniques presented in this article. This console application can be run from the command line with parameters allowing it to be launched during installations.

Currently, the following VS.NET versions are supported:

  • VS.NET 2002
  • VS.NET 2003

Background

Basic familiarity with Visual Studio .NET and C# is necessary for this article. Experience on DTE and Reflection is a plus.

Code

There is one class in this code sample, which provides a single method called RegisterControls. It takes two parameters:

  • VS.NET version (using DTEVersion enum)
  • Controls to be added (using VSControl struct)

DTE stands for Design Time Environment; it is basically the extensibility object model of VS.NET.

DTEVersion is a flag's enum that specifies VS.NET versions to be used.

[Flags]
public enum DTEVersion
{
                                          None      = 0x0000,
    [Description("VisualStudio.DTE.7")]   VS2002    = 0x0001,  //VS.Net 2002

    [Description("VisualStudio.DTE.7.1")] VS2003    = 0x0002,  //VS.Net 2003

}

VSControl struct is used to specify which controls will be added.

// Defines the name of the toolbox tab on which controls

// are located. If IsToolBoxTab is set true, a new toolbox

// tab will be created with the value of this field.

public string TabName;

// The path of the assembly which has controls in it.

public string ControlPath;

// The developer can give the type of the control,

// so the assembly path can be taken automatically.

public Type Control;

// If it is necessary to create a new toolbox tab,

// set IsToolBoxTab to true.

public bool IsToolBoxTab;

This struct has three constructors for different purposes:

public VSControl(Type control, string tabName)

The control will be added to specified toolbox tab.

public VSControl(string assemblyPath, string tabName)

All controls in the assembly will be added to the specified toolbox tab.

public VSControl(string tabGroupName)

A new empty tab will be created in the toolbox.

RegisterControls method first determines the version of DTE and gets the instance of a suitable one.

if((dteVersion & DTEVersion.VS2002) > DTEVersion.None)
{
    // Vs.Net 2002 is available

    dte = GetDesignTimeEnvironment(DTEVersion.VS2002, ref alreadyCreated;
    if(dte != null)
        RegisterControls(dte, alreadyCreated, controls);
    else
        return null;
}

GetDesignTimeEnvironment function returns an instance of either VS.NET 2002 or VS.NET 2003. If there is already an instance running, a reference to that instance is returned. If no instances are found, a new instance is created. When a running instance is found, the next step is to determine whether the toolbox is available. When VS.NET is running, it is not possible to add controls to the toolbox.

string progID = GetEnumDescription(dteVersion);
DTE result;
try
{
    // Determine if there is a running VS.Net instance

    result = (DTE) Marshal.GetActiveObject(progID);
    
    // Try to show properties window to see whether

    // Vs.Net is running a project

    try
    {
        // We have a running and usable Vs.Net instance

        result.ExecuteCommand("View.PropertiesWindow", "");
        alreadyCreated = true;
    }
    catch
    {
        // Running Vs.Net instance state is not usable

        // because it is running a project

        result = null;
    }
}
catch 
{
    // No running instances of Vs.Net is found

    result = null;
}

If there is no running instance of Visual Studio .Net, or the existing one is unusable, we create a new one.

if(result == null)
{
    // Create a new VS.Net instance

    Type type = Type.GetTypeFromProgID(progID);
    if(type != null)
        result = (DTE) Activator.CreateInstance(type);
}

Once we have a suitable DTE, RegisterControls method is called with a reference to the selected DTE. In the RegisterControls method, we get the reference to the toolbox window, then retrieve the tabs collection.

Window toolbox = dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
ToolBoxTabs tabs = ((ToolBox) toolbox.Object).ToolBoxTabs;

Depending on the developer�s selection, either a new tab is added, or an existing tab is used when creating the controls.

tab = GetToolBoxTab(tabs, control.TabName);

if(tab != null &&  !control.IsToolBoxTab)
{
    tab.Activate();
    tab.ToolBoxItems.Item(1).Select();
    tab.ToolBoxItems.Add("MyControls", control.AssemblyPath, 
             vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
}

After we finish adding controls, we close the VS.NET instance only if we created it.

if(!alreadyCreatedDTE)
    dte.Quit();

It is possible to build quickly a console application that uses the code in this article to add controls easily to the toolbox.

class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        string assemblyPath = @ "C:\Controls\SampleControls.dll";
        string tabName = "Sample Controls";

        VSControl sampleTabGroup = new VSControl(tabName);
        VSControl sampleControl1 = 
                        new VSControl(typeof(SampleControl), tabName);
        VSControl sampleControl2 = new VSControl(controlPath, tabName);
            
        bool result = DevEnvironment.RegisterControls(
            DTEVersion.VS2002 | DTEVersion.VS2003, 
            sampleTabGroup, 
            sampleControl1, 
            sampleControl2);
        
        if(result)
            Console.WriteLine("Controls added succesfully.");
        else
            Console.WriteLine(
                  "Controls couldn't be added. There is no VS.Net installed.");

        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();
    }
}

It is possible to extend this sample console application to accept parameters from the command prompt, so that a setup project can run it to add specified controls during installation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Serkan Inci


Member
Serkan Inci
Software Developer
devBiz Business Solutions
http://www.devmail.net
Occupation: Web Developer
Location: Turkey Turkey

Other popular Macros and Add-ins articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralThere is a free tool to do that on VS2005 Pinmemberwww.issoft.eu7:16 19 Aug '07  
GeneralRe: There is a free tool to do that on VS2005 PinmemberRob Smiley0:48 2 Jul '08  
Generaladding a control in vs2005 toolbox PinmemberJustALearner6:03 25 May '07  
GeneralIssues for vs2005 PinmemberOlanB8:13 18 Apr '07  
GeneralToolbox in Visual Studio 2005 PinmemberJavier Aranda13:19 7 Sep '05  
GeneralRe: Toolbox in Visual Studio 2005 Pinmemberwmmihaa4:49 3 Jul '06  
GeneralRe: Toolbox in Visual Studio 2005 Pinmemberramasua6:43 17 Jan '07  
GeneralStrange!!! PinmemberArash Sabet8:21 1 May '05  
GeneralIssues with sample code Pinmembersteven-fowler10:00 14 Mar '05  
GeneralAdding a single control to a toolbox tab Pinmemberbobdupuy_fr0:08 19 Jan '05  
GeneralRe: Adding a single control to a toolbox tab PinmemberSerkan Inci1:28 19 Jan '05  
GeneralRe: Adding a single control to a toolbox tab Pinmemberbobdupuy_fr6:34 19 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jan 2005
Editor: Sumalatha K.R.
Copyright 2005 by Serkan Inci
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project