1: Imports System
2: Imports EnvDTE
3: Imports System.Diagnostics
4:
5: Public Module Collapse
6: Sub CollapseAll()
7: ' Get the the Solution Explorer tree
8: Dim UIHSolutionExplorer As UIHierarchy
9: UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
10: ' Check if there is any open solution
11: If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
12: ' MsgBox("Nothing to collapse. You must have an open solution.")
13: Return
14: End If
15: ' Get the top node (the name of the solution)
16: Dim UIHSolutionRootNode As UIHierarchyItem
17: UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
18: UIHSolutionRootNode.DTE.SuppressUI = True
19: ' Collapse each project node
20: Dim UIHItem As UIHierarchyItem
21: For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
22: 'UIHItem.UIHierarchyItems.Expanded = False
23: If UIHItem.UIHierarchyItems.Expanded Then
24: Collapse(UIHItem)
25: End If
26: Next
27: ' Select the solution node, or else when you click
28: ' on the solution window
29: ' scrollbar, it will synchronize the open document
30: ' with the tree and pop
31: ' out the corresponding node which is probably not what you want.
32: UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
33: UIHSolutionRootNode.DTE.SuppressUI = False
34: End Sub
35:
36: Private Sub Collapse(ByVal item As UIHierarchyItem)
37: For Each eitem As UIHierarchyItem In item.UIHierarchyItems
38: If eitem.UIHierarchyItems.Expanded AndAlso eitem.UIHierarchyItems.Count > 0 Then
39: Collapse(eitem)
40: End If
41: Next
42: item.UIHierarchyItems.Expanded = False
43: End Sub
44: End Module