Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 23rd, 2007, 07:27 AM
Registered User
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# adding custom controls in vs toolbox


I have developed some custom controls and now im doing the setup. I need to insert them into vs toolbar at the setup. For this i created a console application which is launched before setup is done.

I used DTE and some Reflection.

The main ideea is that i look if vs is opened then i get the toolbar object create a tab and add the controls from the assembly. If the vs is not opened or is opend but is running and you dont have acces to the toolbox object (for exemple is running the some code for debug) i create a new instance of vs and add the tab and the controls.

Well my problem is when i create a new instance. I get the toolbox object i add the tab but the controls r not added in the tab. I cant find the problem.

Heres the part of interest for this job.

1. Getting the vs instance :::

public static DTE GetDesignTimeEnvironment(DTEVersion dteVersion, ref bool alreadyCreated)
            alreadyCreated = false;
            string progID = GetEnumDescription(dteVersion);

            DTE result;
                //Determine if there is an open VS.Net
                result = (DTE) Marshal.GetActiveObject(progID);

                //Try to show properties window, if it fails VS.Net is in run-time
                //you have to open a new VS.Net
                    result.ExecuteCommand("View.PropertiesWindow", "");
                    alreadyCreated = true;
                catch //You have to open a new VS.Net
                    result = null;
            catch //There is no open VS.Net
                result = null;

            if(result == null)
                //Open a new VS.Net
                Type type = Type.GetTypeFromProgID(progID);
                if(type != null)
                    result = (DTE)Activator.CreateInstance(type);

            return result;

2. Try to insert them :::

internal static bool RegisterControls(DTE dte, bool alreadyCreatedDTE, VSControl[] controls)
            Window toolbox = dte.Windows.Item(EnvDTE.Constants.vsWindowKindTool box);
            ToolBoxTabs tabs = ((ToolBox) toolbox.Object).ToolBoxTabs;
            dte.ExecuteCommand("View.PropertiesWindow", "");

            foreach(VSControl control in controls)
                ToolBoxTab tab;
                    if(GetToolBoxTab(tabs, control.TabName) == null)
                        tab = tabs.Add(control.TabName);
                    tab = GetToolBoxTab(tabs, control.TabName);
                    if(tab != null && !control.IsToolBoxTab)
                        tab.ToolBoxItems.Add("MyControls", control.AssemblyPath, vsToolBoxItemFormat.vsToolBoxItemFormatDotNETCompo nent);


            return true;

private static ToolBoxTab GetToolBoxTab(ToolBoxTabs tabs, string tabName)
            foreach(ToolBoxTab tab in tabs)
                if (smenglish.CompareInfo.Compare(tab.Name, tabName, CompareOptions.IgnoreCase) == 0)
                    return tab;

            return null;

It seems that in the GetToolBoxTab(..) when vs is not opend and i create an instance each tab.Name trows execptions. I guess the toolbox object is not avaible. What can i do ?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Controls kcis8rm ASP.NET 2.0 Basics 2 July 2nd, 2007 04:07 PM
how to add controls to the visual studio toolbox luisjeronimo BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 June 28th, 2007 04:44 PM
Adding Custom Controls to the Studio's toolbar devdev Visual Studio 2005 0 May 21st, 2006 04:17 PM
Custom Controls apry BOOK: Beginning ASP.NET 1.0 2 May 23rd, 2004 11:04 PM
Adding controls to Toolbox results in two entries DolphinBay VS.NET 2002/2003 4 April 2nd, 2004 07:56 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.