Click here to Skip to main content
11,413,648 members (54,205 online)
Click here to Skip to main content
Technical Blog

VS Solution Explorer – Collapse All

, 2 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
I never knew I needed this until I saw it was a feature in a ReSharper demo.  After a quick search and I found a macro that has been around for years.  Slap to the head!  The amount of time I spend collapsing all my solution folders and this existed the whole time! I never [...]

I never knew I needed this until I saw it was a feature in a ReSharper demo.  After a quick search and I found a macro that has been around for years.  Slap to the head!  The amount of time I spend collapsing all my solution folders and this existed the whole time!

I never use macros, but even I was able to quickly get this working.

  • Open Tools->Macros->Macro Explorer (Alt-F8)
  • Right click MyMacros
  • Select New Module
  • Select default template and name to VSMacros
  • Right-click VSMacros and click New Macro
Copy and paste this over the opened macro file
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module VSMacros
Sub SolutionCollapseAll()

‘ 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 IfGet 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
  • You may now execute the macro via the Macro Explorer.  Click the folder you wish to collapse (or the Solution root to collapse everything).  Right click and run the macro.  To set a shortcut continue on with the steps below…
  • Click Tools->Options->Environment->Keyboard
  • Search for your macro in the Show commands containing textbox
  • Select your macro, keep scope global, and in the assign macro keys textbox enter your preferred shortcut (I used “ALT-CTRL-\”)
  • Click ASSIGN and then OK
  • To use, select a node and then ALT-CTRL-\.  To collapse the entire solution, select the root of the solution.

Reference: http://www.codeproject.com/KB/macros/collapseall.aspx?df=100&forumid=7565&exp=0&select=396167

License

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

Share

About the Author

Chris_Green
Architect Avaya Inc.
Ireland Ireland
Formerly a C++ client developer, nowadays I'm all about C# and ASP.NET. Over the years I have mastered some and played with many aspects of .NET.

Follow my blog as I catalogue the more arcane problems I encounter and their solutions at CodingLifestyle.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
tratak, 29-Apr-11 12:42
membertratak29-Apr-11 12:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.2 | Last Updated 2 Apr 2011
Article Copyright 2011 by Chris_Green
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid