Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal

Search

Search

Visual Studio Macro for Collapsing All Items in Solution Explorer

Note
This post originally appeared on my MSDN blog:

Since I no longer work for Microsoft, I have copied it here in case that blog ever goes away.

Along with my Visual Studio macros for unloading/reloading projects in a solution, another macro that I use just as much, if not more frequently, is my CollapseAllItems() macro:

Public Sub CollapseAllItems()
    Dim solutionExplorer As Window = _
        DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)

    DTE.SuppressUI = True

    Try
        Dim solutionHierarchy As UIHierarchy = solutionExplorer.Object

        For Each item As UIHierarchyItem _
            In solutionHierarchy.UIHierarchyItems

            CollapseItem(item, solutionHierarchy)
        Next
    Catch ex As Exception
        WriteOutput("Error collapsing all items: " _
            & ex.Message)

    Finally
        DTE.SuppressUI = False
    End Try
End Sub

The CollapseItem() method is used to recursively collapse each item in the hierarchy:

Private Sub CollapseItem( _
    ByVal item As UIHierarchyItem, _
    ByVal solutionHierarchy As UIHierarchy)

    For Each child As UIHierarchyItem In item.UIHierarchyItems
        CollapseItem(child, solutionHierarchy)
    Next

    If (item.UIHierarchyItems.Expanded = True) Then
        WriteOutput("Collapsing item (" & item.Name & ")...")
        item.UIHierarchyItems.Expanded = False

        ' HACK: Known bug in Visual Studio 2005
        ' http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=114597
        If (item.UIHierarchyItems.Expanded = True) Then
            item.Select(vsUISelectionType.vsUISelectionTypeSelect)
            solutionHierarchy.DoDefaultAction()
        End If
    End If
End Sub
Update (2010-08-25)
In my original post, the If block in CollapseItem was mistakenly nested inside the For Each loop. While this worked (I've been using it that way for years), it certainly wasn't optimal and, more importantly, it also was the source of some confusion (see Keith Robertson's comment on this post).

While it is great that Visual Studio "synchronizes" the Solution Explorer window to show the current file in the solution hierarchy, in large Visual Studio solutions, things can get a bit bewildering at times if many of the projects are expanded down to the level of individual files.

Perhaps in a future version of Visual Studio, we'll have the ability to right-click the solution in Solution Explorer and then click something like Collapse All. Until then, I don't see me giving up my dependency on this macro anytime soon.

Comments

  1. # re: Visual Studio Macro for Collapsing All Items in Solution Explorer

    June 17, 2010 10:37 AM
    Steve Sharp

    Thanks! Very helpful!

    I had to change "WriteOutput" to "Console.Write" in VS 2008.

  2. # re: Visual Studio Macro for Collapsing All Items in Solution Explorer

    June 17, 2010 11:17 AM
    Jeremy Jameson
    Gravatar

    The WriteOutput function is provided in a previous blog post:

    blogs.msdn.com/.../tracing-and-logging-from-visual-studio-macros.aspx

  3. # re: Visual Studio Macro for Collapsing All Items in Solution Explorer

    August 24, 2010 7:05 AM
    Keith Robertson

    In CollapseItem(), did you really intend to perform all the operations 'item' (the input parameter) instead of 'child' (the loop variable)?

  4. # re: Visual Studio Macro for Collapsing All Items in Solution Explorer

    August 25, 2010 5:15 AM
    Jeremy Jameson
    Gravatar

    @Keith,

    Yes, most of the operations in CollapseItem are performed on 'item' (the input parameter), but I can definitely see why the original code caused some confusion. The "If" block should not have been nested inside the "For Each" block. While it worked, it certainly wasn't optimal.

    I've updated the macro code and verified it still works.

    Thanks for the catch!

  5. # Click & Solve » Visual Studio Macro for Collapsing All Items in Solution Explorer

    March 11, 2009 10:37 AM
    www.clickandsolve.com

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Apple

Grapes
Apple
Strawberry
Watermelon
Cherries
Pear
 
Please add 7 and 7 and type the answer here: