Geeks With Blogs

Scott Kuhl Warning: I may have no idea what I am talking about!

Visual Studio has this annoying habit of expanding the solution folder tree, especially when synchronizing with Visual SourceSafe.  Even without that problem I sometimes find myself with many expanded tree nodes in the Solution Explorer and I want a quick way to collapse them all.

So I searched for a macro based solution to the problem and came up with the VS.Net Macro: Collapse All from Captain LoadTest.  This macro was not designed to collapse a non-expanded folder's sub items.  So I made a few quick changes to get the macro to walk the entire tree and collapse every item that was expanded.

But is was not working.  Some items just were not collapsing, so I ended up with more items expanded than when I started because Visual Studio will expand the items while you walk the tree.  It turns out I ran into a bug in Visual Studio 2005 that should have been fixed in SP1 and that was marked as fixed by Microsoft, but still is not fixed.  Carlos J. Quintero mentioned a work-around in a Microsoft forum that seems to do the job.

The code to collapse everything does take longer to run (10 seconds across a very large solution, compared to 1 second with the original script) but it does work now.

So now that everything is working here is the code:

 

    Sub CollapseAll()

        ' 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

        ' Collapse each project node
        Collapse(rootNode, solutionExplorer)

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

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

                ' Re-cursive call
                Collapse(innerItem, solutionExplorer)

                ' 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
Posted on Monday, April 9, 2007 10:43 AM .NET , Software | Back to top


Comments on this post: Visual Studio 2005 Collapse All Macro

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
This rocks my world. Works great!
Left by Amazing on May 10, 2007 12:20 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thanks a ton..I've been searching this since a long time...

You've helped a lot!!

Cheers!!
Left by Umaima on Jun 10, 2007 11:46 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Marvellous - the standard CollapseAll macro that is quoted elsewhere means that sub-nodes are still expanded when a node is reopened.

But this works properly - all nodes are recursively collapsed fully.

Thanks so much !!
Left by Mr.V.Grateful on Jun 21, 2007 8:39 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Does not work for me. I run the macro and nothing happens. No error message. No projects collapsed. Nothing.
Left by JT on Aug 02, 2007 11:46 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Got it working. Need SP1 to fix macro bug in Vs2005. One downside with this macro is that Visual Studio calls my source control to check the check-in status of EVERY SINGLE file in the solution after the macro runs. This takes 2-3 minutes to do!!!!!!!!!
Left by JT on Aug 03, 2007 8:36 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Great little macro. However, I had the same issue as JT above where it took several minutes to run as the status of EVERY file was checked. To get around this, I created another Macro and made a very minor alteration so that it only looks into a node if that node is expanded. Obviously this won't collapse nodes that are underneath an already collapsed node, but for my purposes I'll use it more often. The only change I made was within the Collapse sub routine just after the looping begins, instead of just:

If innerItem.UIHierarchyItems.Count > 0 Then

I made it:

If innerItem.UIHierarchyItems.Count > 0 And innerItem.UIHierarchyItems.Expanded Then

This resulted in a decent speed boost for me.

Again, thanks for the nice, recursive macro.
Left by JStuart on Aug 21, 2007 5:15 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Seriously ... i could collapse the list manually in less time...

I dont mind it runnning through all nodes, but in my oppinion it shoudl be able to sort out wether an item is already collapsed or not.
Left by Helge on Sep 18, 2007 9:38 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
I was working at editing another macro to do this, but after wasting a couple of hours I decided that somebody else would probably have figured it out. And sure enough here it is!
Thank you very much for this nice macro.
Left by sansay on Dec 05, 2007 1:59 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
I found this macro to be extremely useful, although somewhat slow at time due to our extremely large solutions.

I modified this macro (or a similar variant found on another website) to recursively collapse down to a user specified depth. The code I work with has such extensive folder structures that often do not need to be collapsed since I never visit most of it anyways.

I found that a depth of 2 is often very fast and usually suitable, but set it to 3 to cover all of the bases. But hey, you can select your own depth!

Enjoy.

Sub Collapse()

'Set the depth of collapsing
Dim depth As Integer
depth = 3

' 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

' Collapse each project node
Collapse(rootNode, solutionExplorer, 0, depth)

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

rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False

End Sub

Private Sub Collapse(ByVal item As UIHierarchyItem, _
ByRef solutionExplorer As UIHierarchy, _
ByRef Level As Integer, ByRef Depth As Integer)

'Collapse to the right Depth
If Level < Depth Then

For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then

' Re-cursive call
Collapse(innerItem, solutionExplorer, Level + 1, Depth)

' 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 If
End Sub
Left by nayr on Dec 07, 2007 3:35 AM

# Much faster solution
Requesting Gravatar...
CoolCommands contains a command for collapsing the solution explorer nodes. It is very fast.
http://weblogs.asp.net/gmilano/archive/2006/05/10/446010.aspx
Left by Robert on Jan 09, 2008 1:54 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Dude, you ROCK!! I have been looking for a way to do this for a very long time. This is going to save me so much time. Much appreciated!

Thanks,
Pete Soheil
DigiOz Multimedia
Left by Pete on May 16, 2008 1:34 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Expanding should work pretty much the same. Double check your code.
Left by Scott Kuhl on Aug 15, 2008 5:00 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thanks a lot for this. This is a real time saver!
Left by Eden Ridgway on Aug 19, 2008 7:06 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Very nice.
Left by Eric A. Duncan on Nov 02, 2008 7:26 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
just do ctrl + m, ctrl + o its built in!
Left by dfgef on Jan 10, 2009 4:30 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
good start, but damn was it slow!
i modified it so that it does not go deeper than it needs to as far as recursion goes and now it works for me in 1-2 seconds


Sub CollapseAll()

' 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

' Collapse each project node
Collapse(rootNode, solutionExplorer)

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

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

' Re-cursive call
If innerItem.UIHierarchyItems.Expanded Then
Collapse(innerItem, solutionExplorer)
End If

' Collapse
innerItem.UIHierarchyItems.Expanded = False
'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
Left by sonicsoul on May 08, 2009 3:21 PM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thanks you guys so much !.
Left by luhuy on May 09, 2009 3:09 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
thank alot!!! i have looked for this macro for along time
Left by aviad on May 12, 2009 7:36 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Very nice. I love it
Left by Justin on Jun 15, 2009 11:14 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Cheers!

Thanks a lot!!!!
Left by Roman K / Ukraine on Jul 16, 2009 12:04 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Perfect. Works great with my very large, source-controlled solution in VS2008. Thanks!

-Tony
Left by Tony Evans on Dec 03, 2009 6:05 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Here is another collapse all macro I have used successfully (HINT: use the instructions for the 2005 version if you don't know how to create a VS macro):

http://it.toolbox.com/blogs/think-clients/visual-studio-macro-collapse-solution-explorer-18035
Left by Answering Service on Oct 25, 2010 1:58 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
The *, + and - on the numeric pad controls this. After pressing ctrl/alt/L just position on the folder to expand and press *. To collapse press -. Then if you have collapsed some sub-folders, press + to return to your partially collapsed version.
Left by EddyQ on Mar 03, 2011 1:47 PM

# re: Thx sonicsoul
Requesting Gravatar...
5/8/2009 3:21 PM | sonicsoul:

That's ultra very fast!
Thanks guy.
Left by Edjlle on Apr 29, 2011 5:40 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thanks a lot!!
Left by imagemoz on Apr 30, 2011 6:57 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thank you very much. Works like a charm!
Left by Paul Bohuch on Sep 08, 2011 9:01 AM

# re: Visual Studio 2005 Collapse All Macro
Requesting Gravatar...
Thanks - a good little time-saver
Left by Paul CJ on Feb 16, 2012 11:13 PM

Your comment:
 (will show your gravatar)


Copyright © Scott Kuhl | Powered by: GeeksWithBlogs.net | Join free