Click here to Skip to main content
15,890,282 members
Articles / Programming Languages / VBScript

VS Solution Explorer – Collapse All

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Apr 2011CPOL1 min read 14.7K   2   1
VS Solution Explorer – Collapse All

I never knew I needed this until I saw it was a feature in a ReSharper demo. After a quick search, I found a macro that has been around for years. Slap to the head! The amount of time I spent 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.

VB.NET
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module VSMacros
Sub SolutionCollapseAll()

‘ Get 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 selected 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)


Written By
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
tratak29-Apr-11 11:42
professionaltratak29-Apr-11 11:42 

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.