(Yes, I know that you marked the other answer as accepted, but technically this did not answer your question with the encoding, although it gave you an answer. This, however, is what you originally asked.)
Actually, I don’t think it would work in any version of Visual Studio, because the logic in your Collapse function is wrong. Same thing with the initial loop in the project (which I'm not sure why you just didn't pass the node solution to the collapse function anyway ...)
In particular, you check to see if it has expanded before delving into the children, so if the folder crashes but its children are expanded, it will not go inside and will not hide the children, since the check will skip it. Just check the bill. In the end, you want to delve into everything in order to collapse.
Here is the version that I created from your point of view as a starting point. I also added the ability to re-select the selected item, if any. I like to have the item I'm working on. And if I want everything to crash, I either select the root node or deselect it, in both cases it selects the root (node solution).
I also added a MultiBeep function for audible feedback, since I'm not a fan of messages. 2 beeps are successful, 3 means you do not have an open solution.
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
NTN
Mark
source share