I found the CollapseAll macro online that has worked for me in vs2005 and vs2008. However, this half way works in vs2010. It looks like it only collapses the top nodes and not any subnodes that may be expanded? any ideas?
Thanks, rod.
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)
UIHSolutionRootNode.DTE.SuppressUI = True
' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
'UIHItem.UIHierarchyItems.Expanded = False
If UIHItem.UIHierarchyItems.Expanded Then
Collapse(UIHItem)
End If
Next
' 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)
UIHSolutionRootNode.DTE.SuppressUI = False
End Sub
Private Sub Collapse(ByVal item As UIHierarchyItem)
For Each eitem As UIHierarchyItem In item.UIHierarchyItems
If eitem.UIHierarchyItems.Expanded AndAlso eitem.UIHierarchyItems.Count > 0 Then
Collapse(eitem)
End If
Next
item.UIHierarchyItems.Expanded = False
End Sub
End Module
(Yes, I know you marked the other answer as accepted, but technically that wasn't answering your coding question, albeit it did give you an answer. This however is what you originally asked.)
Actually, I don't think this would have worked in any version of Visual Studio because the logic in your Collapse function is wrong. Same with the initial loop in the project (which I'm not sure why you just didn't pass the solution node to the collapse function anyway...)
Specifically, you're checking if it's expanded before delving in to the children, so if a folder is collapsed but its children are expanded, it won't go inside and collapse the children as your check causes that to be skipped. Just check the count. After all you DO want to delve in to everything to collapse.
Here's a version I created using yours as a starting point. I also added the ability to re-select the selected item if there is one. I like having the item I'm working on selected. And if I want everything collapsed, I either select the root node, or I deselect everything, in both cases it then selects the root (the solution node).
I also added the MultiBeep function for audible feedback since I'm not a fan of message boxes. 2 beeps is success, 3 means you don't have a solution open.
Sub CollapseSolutionTree()
' Get the the Solution Explorer tree
Dim SolutionExplorer As UIHierarchy = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (SolutionExplorer.UIHierarchyItems.Count = 0) Then
MultiBeep(3)
Return
End If
' Get the selected node (if any)
Dim SelectedNode As UIHierarchyItem
Try
SelectedNode = SolutionExplorer.SelectedItems(0)
Catch
End Try
' Get the top node (the name of the solution)
Dim SolutionNode As UIHierarchyItem = SolutionExplorer.UIHierarchyItems.Item(1)
SolutionNode.DTE.SuppressUI = True
' Collapse the solution tree
CollapseSolutionExplorerNode(SolutionNode)
' If there was a selected item before the command, re-select it. Otherwise select the solution node.
If SelectedNode isnot Nothing
SelectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
Else
SolutionNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End If
SolutionNode.DTE.SuppressUI = False
MultiBeep(2)
End Sub
Private Sub CollapseSolutionExplorerNode(ByVal Folder As UIHierarchyItem)
For Each Subfolder As UIHierarchyItem In Folder.UIHierarchyItems
If Subfolder.UIHierarchyItems.Count Then CollapseSolutionExplorerNode(Subfolder)
Next
Folder.UIHierarchyItems.Expanded = False
End Sub
Private Sub MultiBeep(Count As Integer, Optional Spacing As Integer = 100)
Beep
For I = 2 to Count
System.Threading.Thread.CurrentThread.Sleep(Spacing)
Beep
Next
End Sub
HTH,
Mark
There's a VS2010 extension at Sara Ford's blog:
http://blogs.msdn.com/b/saraford/archive/2010/05/13/collapse-selection-in-solution-explorer-extension-7.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With