All pastes #931560 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-03-07 08:16 UTC
rendered paste body
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module CollapseAll
    Sub CollapseNode(ByRef item As UIHierarchyItem)

        Dim subitem As UIHierarchyItem
        For Each subitem In item.UIHierarchyItems
            If (subitem.UIHierarchyItems.Expanded = True) Then
                CollapseNode(subitem)
                subitem.UIHierarchyItems.Expanded = False
            End If
        Next

    End Sub

    Sub CollapseAll()

        ' Get the the Solution Explorer tree
        Dim UIHSolutionExplorer As UIHierarchy
        UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

        ' Check if there is any open solution
        If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
            ' MsgBox("Nothing to collapse. You must have an open solution.")
            Return
        End If

        ' Get the top node (the name of the solution)
        Dim UIHSolutionRootNode As UIHierarchyItem
        UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)

        CollapseNode(UIHSolutionRootNode)

        ' 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.
        UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)

    End Sub
End Module