Visual Studio Collapse Selected Node

Over the past few years I've seen developers struggling with the missing feature of collapse all projects in visual studio, there are some macros and add-ins around the net to solve this issue but non of them was good enough for me as most of the solutions collapsed all of the projects.

In my day to day I'm teaching and working with Smart Client Software Factory, the SCSF solution is build using Folders (Source, Infrastructure ... ) which happen to collapse also using the various solutions.

Well, Its time to build a simple macro to do the job my way...

Create a new VS macro solution and paste the following code, assign a hot key (I use Alt-Ctrl-/) and you are done.

Select a node on the solution folder that you want its child to collapse and hit the hot key.

Imports System Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module CollapseSelectedNode
  Sub CollapseSelected()
     ' 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

       ' Get the selected node
        Dim UIHSolutionRootNode As UIHierarchyItem
        UIHSolutionRootNode = UIHSolutionExplorer.SelectedItems(0)

        UIHSolutionRootNode.DTE.SuppressUI = True

        ' Collapse each project node
        Dim UIHItem As UIHierarchyItem
        For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
          If UIHItem.UIHierarchyItems.Count > 0 And UIHItem.UIHierarchyItems.Expanded Then
             ' As of a Bug in VS 2005 you have to use the following lines
             ' instead of using UIHItem.UIHierarchyItems.Expended = False
              UIHItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
              UIHSolutionExplorer.DoDefaultAction()
          End If
        Next

        UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
        UIHSolutionRootNode.DTE.SuppressUI = False
   End If
  End Sub
End
Module

2 Comments

  • ' instead of using UIHItem.UIHierarchyItems.Expended = False
    UIHItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
    UIHSolutionExplorer.DoDefaultAction()

    I want to write a macro that collapses EVERYTHING in the tree. DoDefaultAction may collapse the node and it may load something even if UIHierarchyItems.Count > 0 and UIHierarchyItems.Expended = True. Very frustrating. Anyone know a way to do what I want in VS 2005?

  • Great feature. Thanks!!

Comments have been disabled for this content.