Click here to Skip to main content
15,921,941 members
Articles / Programming Languages / Visual Basic

Collapsing All Projects in Solution With a Custom Button in a ToolBar

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
20 Apr 2009CPOL3 min read 18.6K   7   5
How to collapse all projects in solution with a custom button in a ToolBar

[This article is a collage of the previous two articles: first and second, and shows how to create a custom button in a toolbar which collapses all your solution projects. Useful if you have solutions with more than 50 projects, like in my case.]

Part 1: Collapse All Projects in the Solution Explorer (Visual Studio)

If you work in large projects usually, you can end with up to 30, 40, 50 projects or more inside a single solution.

If that's your case, it is sometimes a pain to work with the solution explorer. In addition to that, Visual Studio sometimes expands the full solution when it opens it. How much time have you wasted clicking project by project just to get a tiny, collapsed solution?

No more!

Thanks to Edwin Evans, we have a simple VB Macro that collapses the entire solution. You can find the article here.

Just go to Tools -> Macros -> New Macro Project, rename it as you like, and paste the VB code there. Afterwards, you can create a custom ToolBar in the VisualStudio IDE and add your new macro as a button there.

Et voilá, one click collapse for your entire solution!

I've tried it and it works, at least in Visual Studio 2008. I'll post another article today on how to customize your toolbar… keep reading!

PS: For your quick reference, I paste Edwin Evans' code below:

Sub CollapseAll()
     ' Get the Solution Explorer tree
     Dim UIHSolutionExplorer As UIHierarchy
     UIHSolutionExplorer = DTE.Windows.Item( _
     ' Check if there is any open solution
     If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
         ' MsgBox("Nothing to collapse. You must have an open solution.")
     End If
     ' Get the top node (the name of the solution)
     Dim UIHSolutionRootNode As UIHierarchyItem
     UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
     ' Collapse each project node
     Dim UIHItem As UIHierarchyItem
     For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
         UIHItem.UIHierarchyItems.Expanded = False
     ' Select the solution node, or else when you click
     ' on the solution window
     ' scrollbar, it will synchronize the open document
     ' with the tree and pop
     ' out the corresponding node which is probably not what you want.
 End Sub

Part 2: How to Create a Custom ToolBar in Visual Studio

It is sometimes necessary to add custom ToolBars to the Visual Studio IDE. This post will show you how to do it…

1. Create a New ToolBar

Just go to Tools –> Customize, you will find a new window like this one:


Click on the ToolBars tab, and then in the “New” button. It will ask for the name of the new ToolBar, in our case, the name was: “Macros”. Just type it and press return.

Now, your new ToolBar appears in the list on your left. Be sure to check it, so it will appear in the VisualStudio IDE (you can make it a floating ToolBar or dock it into the upper space for toolbars, whatever you want).

2. Add a new button to the ToolBar

Go to Tools –> Customize again, but this time click on the “Commands” tab, it´s something like this:


You have Command Categories on your left, and all the commands belonging to the selected category on your right.

To create a new button for one of that commands, just Drag&Drop the desired command to your new ToolBar. Easy as that.

3. Customize the Appearance of the Button

Again, go to Tools –> Customize –> Commands Tab.

This time, click on the “Rearrange Commands” button. A new window will show with all the customizing options for your menus and toolbars. Just like this one:


In this window, you can customize many things, like button order, appearance, icons, texts, whatever.

To customize your new ToolBar, just select the “ToolBar” radio button and your recently created ToolBar in the combo box of your right.

The list on the bottom-left part of the windows will show all the buttons the toolbar contains, and on the bottom-right part, you have customizing buttons: add, delete, move up and down and modify.

This last option allows you to change button text (Name), icons, and all that stuff.

Hope you liked it!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Spain Spain
Inaki Ayucar is a Microsoft MVP in DirectX/XNA, and a software engineer involved in development since his first Spectrum 48k, in the year 1987. He is the founder and chief developer of The Simax Project ( and is very interested in DirectX/XNA, physics, game development, simulation, C++ and C#.

His blog is:

To contact Inaki:

Comments and Discussions

GeneralThoughts Pin
PIEBALDconsult20-Apr-09 5:50
mvePIEBALDconsult20-Apr-09 5:50 
GeneralRe: Thoughts Pin
Inaki Ayucar20-Apr-09 6:04
Inaki Ayucar20-Apr-09 6:04 
GeneralRe: Thoughts Pin
PIEBALDconsult20-Apr-09 6:24
mvePIEBALDconsult20-Apr-09 6:24 
GeneralRe: Thoughts Pin
Inaki Ayucar20-Apr-09 6:30
Inaki Ayucar20-Apr-09 6:30 
GeneralRe: Thoughts Pin
Inaki Ayucar20-Apr-09 6:34
Inaki Ayucar20-Apr-09 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.