Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - CollapseAll.gif

Introduction

Visual Studio .NET has a cool feature that it will automatically synchronize the Solution Explorer with the open file. One problem though is that if you have a large number of projects, the number of branches can soon get out of control - you can't see the trees for all the branches, so to speak. This is a very simple macro that you can use to collapse all the project nodes in the Solution Explorer.

Macro Code

    Sub CollapseAll()

        ' Get the the Solution Explorer tree

        Dim UIHSolutionExplorer As UIHierarchy
        UIHSolutionExplorer = DTE.Windows.Item( _
            Constants.vsext_wk_SProjectWindow).Object()

        ' Check if there is any open solution

        If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
            ' MsgBox("Nothing to collapse. You must have an open solution.")

            Return
        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
        Next

        ' 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.

        UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

    End Sub
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralGreat Macro!
Darren Boss
6:31 2 Dec '09  
Thank you for posting this. It has been a big help.
GeneralExcellent work, but DPack plugin for vs.net already do it(although slower than your macro solution)
slimzhao
17:04 23 Aug '07  
please reference:
http://www.usysware.com/DPack/Default.aspx

Right click the solution, there's a menu item "Collapse all projects".


in VS2005, there's also a short cut mapped to this: SHIFT + ALT + C

DPack do a boundle of wonderful thing to improve developers' productvity, worth a try.

But, DPack's solution in vs2003 looks very slow compared to your macro's way.

I think it's better to use the macro for real large .sln file.
GeneralVisual Studio 2005 Bug and Fix
Scott Kuhl
7:50 9 Apr '07  
There is a bug in VS 2005 that keeps some nodes from collapsing. The fix and a code enhancement to collapse all sub-nodes can be found here: http://geekswithblogs.net/scottkuhl/archive/2007/04/09/111195.aspx
GeneralRe: Visual Studio 2005 Bug and Fix
Peter Solberg
20:07 23 May '07  
I have made some improvements to Scott's code. My code can collapse selected item and runs faster than Scott's code on a very large solution.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Collapse

Sub CollapseItem()

' Get the the Solution Explorer tree
Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (solutionExplorer.UIHierarchyItems.Count = 0) Then Return End If
' Get the top node (the name of the solution)
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI = True
'Find selected node
Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)

' Collapse each node
Collapse(selectedNode, solutionExplorer)

' Select the selceted node
selectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False
End Sub
Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)

For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then
If innerItem.UIHierarchyItems.Expanded Then ' Re-cursive call
Collapse(innerItem, solutionExplorer)
End If
' Collapse
If innerItem.UIHierarchyItems.Expanded Then innerItem.UIHierarchyItems.Expanded = False If innerItem.UIHierarchyItems.Expanded = True Then ' Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()
End If End If
End If Next
End Sub
Private Function FindSelectedNode(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy) As UIHierarchyItem

If item.IsSelected Then Return item
End If For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then
If innerItem.UIHierarchyItems.Expanded Then ' Re-cursive call
Dim result As UIHierarchyItem
result = FindSelectedNode(innerItem, solutionExplorer)
If Not result Is Nothing Then Return result
End If End If
If innerItem.IsSelected Then Return innerItem
End If
End If Next Return Nothing
End Function

End Module

GeneralVery useful, thanks!
benno_1
14:31 19 Nov '06  
A simple but very useful utility, thanks!
GeneralConfigurable Depth
DrewBurlingame
12:35 14 Feb '06  
I sometimes need to collapse more than just the top level. Here's some code to configure the depth.

Sub CollapseTop()
Collapse(3)
End Sub

Sub CollapseAll()
Collapse(-1)
End Sub

Sub Collapse(ByVal maxDepth As Integer)

' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item( _
Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
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
CollapseLevel(UIHItem, maxDepth)
Next

' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub

Sub CollapseLevel(ByVal node As UIHierarchyItem, ByVal maxDepth As Integer, Optional ByVal currentLevel As Integer = 0)
'would like to be able to determine if this was a folder. But didn't see the option on the object.
If (maxDepth = -1 Or currentLevel < maxDepth) Then
currentLevel = currentLevel + 1
node.UIHierarchyItems.Expanded = False
Dim UIHItem As UIHierarchyItem
For Each UIHItem In node.UIHierarchyItems
CollapseLevel(UIHItem, maxDepth, currentLevel)
Next
End If
End Sub

-- modified at 17:36 Tuesday 14th February, 2006
Generallittle enhancment (you can publish it if you want)
Bnaya Eshet
5:53 12 Jan '06  
Sub CollapseAll()

' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item( _
Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If

' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)

CollapseRec(UIHSolutionRootNode)

' Collapse each project node

' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub

Sub CollapseRec(ByVal UIHParent As UIHierarchyItem)
Dim UIHItem As UIHierarchyItem

If UIHParent Is Nothing Then
Return
End If

' Check if there is any open solution
If (UIHParent.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If

For Each UIHItem In UIHParent.UIHierarchyItems
CollapseRec(UIHItem)

UIHItem.UIHierarchyItems.Expanded = False
Next
End Sub


Bnaya Eshet

Chief Architect at
Wise Mobility
www.wisemobility.com

GeneralGreat. Works with VS 2003
MtF_
11:40 25 Jul '05  
Great makro. Works in VS 2003 as well.

M
Generalexcellent, very useful :)
feline_dracoform
3:29 22 Jun '05  
while i am not a total fan of having the solution explorer follow the open code, i find i need to keep it turned on since it causes more problems turning it off.

as a result a rather large tree ends up open all over the place, making a complete mess. this is ideal Big Grin Rose

zen is the art of being at one with the two'ness
GeneralCollapse the subfolders too
Charles Windhausen
13:38 8 Dec '04  
I modified the macro slightly so it will collapse all subfolders in the solution tree as well...

'------------------------------------------------------------------------------
Sub CollapseMe(oRootItem As UIHierarchyItem)

Dim oChildItem As UIHierarchyItem

For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next

oRootItem.UIHierarchyItems.Expanded = False

End Sub

'------------------------------------------------------------------------------
Sub CollapseAll()
'DESCRIPTION: Colapse all the nodes in the project tree

' Get the the Solution Explorer tree
Dim oSolutionExplorer As UIHierarchy
oSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (oSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If

' Get the top node (the name of the solution)
Dim oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem

' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next

' 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.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub


Charles
GeneralCan it work for ClassView?
Kevin McFarlane
7:29 4 Oct '04  
Hi Edwin,

Have you tried applying your macro to ClassView? For some reason doing this

DTE.Windows.Item(Constants.vsext_wk_ClassView).Object()

always gives you Nothing.

I've also tried:

DTE.Windows.Item(Constants.vsWindowKindClassView).Object()

Same result.

Referencing the ClassView window is OK though. It's only when you apply .Object that you get Nothing.

Kevin

Kevin
GeneralRe: Can it work for ClassView?
Erlend
21:46 6 Feb '05  
Apparently the classview does not implement anything at all to be able to access the tree Mad ,
but here is a trick that could help you out :

There are commands to switch the type of treeview within the classview :

1. View.ClassViewGroupByType
2. View.SortAlphabetically
3. View.SortByAccess
4. View.SortByType

If you call 1. and 4. using

dte.ExecuteCommand("View.ClassViewGroupByType", "");
dte.ExecuteCommand("View.ClassViewSortByType", "");

The tree is collapsed Smile , sadly enough i did not find a way yet to test which one is
active Confused so it could be that you end up with enother sort then you started off with

Brgds,

Erlend



Erlend Robaye

Fluxys N.V.
Belgium
Erland.Robaye@fluxys.net
GeneralAbout ToolButton
Anonymous
9:18 14 Nov '02  
Great macro.
How did you add the ToolButton which says "Collapse All" in the Solution Explorer's toolbar?
Thanks.

PKP
GeneralRe: About ToolButton
Edwin Evans
19:28 14 Nov '02  
Here is a explanation of how to add a button to the toolbar: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q318651#4. Start with step 6.

If you want to do this programmatically, here is what you could do: (I wrote an Add-In version of this same feature in C#)

Command command = commands.AddNamedCommand(addInInstance, "CollapseAll", "CollapseAll", "Collapses all the project nodes in the Solution Explorer", true,
41, // blue arrow, see http://www.j-walk.com/ss/excel/tips/tip40.htm, thanks to John Walkenbach
ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);

// This will add the button to the end of the standard toolbar
// but the user always has the ability to customize this.
CommandBar commandBar = (CommandBar)commandBars["Standard"];
int nPositionEnd = commandBar.Controls.Count + 1;
CommandBarControl commandBarControl = command.AddControl(commandBar, nPositionEnd);

Rose

-Edwin
GeneralRe: About ToolButton
Anonymous
5:40 15 Nov '02  
Hi Edwin,

Thanks for the reply.

I knew how to add a CommandBarControl to a toolbar (via code or using Tools|Customize. However in your screenshot you had added a "CollapseAll" button to a toolbar which seems like part of "Solution Explorer". That toolbar does not seem to be customizable using Tools|Customize.

So how did you do it? I will be incorporating this in an add-in.

You did mention how can I add a toolbutton to the "Standard" toolbar. But how can I add it to a Solution Explorer toolbar? Is there a string name for that toolbar?

I will appreciate your help in this regard.
Thanks.

Paresh
GeneralRe: About ToolButton
Booga
5:13 28 Nov '02  
If you look at the pictures again, you'll see that Edwin's button is *not* inside the Solution Explorer.
GeneralGreat Macro!
Stephen Jones
6:05 4 Oct '02  
This is a great macro, but I wouldn't call the auto-sync feature in the Solution Explorer a "great feature" Eek! .
Thanks,
- Stephen Jones
GeneralRe: Great Macro!
Edwin Evans
6:25 6 Oct '02  
Yes, it can be very annoying actually! I find it useful in some ways but why couldn't they just put a button to sync, or have it turned off by default?

I bet a lot of people hate that feature. Here is a newsgroup thread I ran across recently that reveals some of them...

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&frame=right&th=53f144229cfc6b1d&seekm=3c642a00%241%40rpc1284.daytonoh.ncr.com#link1
GeneralRe: Great Macro!
Booga
3:41 23 Jan '03  
It was great, but not quite what I was looking for. Here's my modifications, which allow the macro to collapse all subfolders of expanded folders as well. It does not deal with expanded subfolders in collapsed folders, as that takes much too long (it actually seemed to hang my VS).
Enjoy!

Sub CollapseNode(ByRef item As UIHierarchyItem)

Dim subitem As UIHierarchyItem
For Each subitem In item.UIHierarchyItems
If (subitem.UIHierarchyItems.Expanded = True) Then
CollapseNode(subitem)
subitem.UIHierarchyItems.Expanded = False
End If
Next

End Sub

Sub CollapseAll()

' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If

' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)

CollapseNode(UIHSolutionRootNode)

' 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.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub
GeneralRe: Great Macro!
Edwin Evans
12:00 21 Jun '05  
Hey, thanks Booga! That's just what I need with the new project I'm working on.

-Edwin
GeneralRe: Great Macro!
Randy87
10:43 23 Jul '07  
Thanks, exactly what I needed.


Last Updated 21 Sep 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010