次の方法で共有


Visual Studio Macro for Collapsing All Items in Solution Explorer

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
        ' https://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

  • Anonymous
    March 11, 2009
    PingBack from http://www.clickandsolve.com/?p=21146

  • Anonymous
    June 17, 2010
    Thanks! Very helpful! I had to change "WriteOutput" to "Console.Write" in VS 2008.

  • Anonymous
    June 17, 2010
    The WriteOutput function is provided in a previous blog post: blogs.msdn.com/.../tracing-and-logging-from-visual-studio-macros.aspx

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

  • Anonymous
    August 24, 2010
    @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!