vendredi 8 juin 2007

Expanding or Collapsing all nodes in the Solution Explorer

From : Expanding or Collapsing all nodes in the Solution Explorer

Expanding or Collapsing all nodes in the Solution Explorer

The following two macros will collapse and expand all of the items in the Solution Explorer. To install them:

  • Open up the Macro Explorer in VS2005 by pressing alt + F8.
  • Expand the My Macros node and add a new macro module. Name it whatever you want.
  • Double click on the new module and, in between "Public Module blah" and "End Module", paste the following macro code...
    Sub CollapseSolExplorerNodes()
' 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
Return
End If

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

' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
UIHItem.UIHierarchyItems.Expanded = False
Next

' Select the solution node, or else when you click
' on the solution windows 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

Sub ExpandSolExplorerNodes()
' 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
Return
End If

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

' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
UIHItem.UIHierarchyItems.Expanded = True
Next

' Select the solution node, or else when you click
' on the solution windows 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

Save the module.

You can now test the new macros by double clicking on them in the Macro Explorer. The IDE will switch to the Solution Explorer and run the macro.

If everything works as it should, now lets assign the macro a hotkey:

  • In Visual Studio, Click "Tools Options"
  • Expand "Environment" and select "Keyboard."
  • In "Show Commands Containing", type "CollapseSol" and you should see the new CollapseSolExplorerNodes macro displayed below.
  • In "Use New Shortcut In", select "Global".
  • Select the "Press Shortcut Key" box and press "alt \"
  • Click [Assign].
  • Now, back in "Show Commands Containing", type "ExpandSol" and you should see the new ExpandSolExplorerNodes macro displayed below.
  • In "Use New Shortcut In", select "Global".
  • Select the "Press Shortcut Key" box and press "alt /"
  • Click [Assign].
  • Click [OK]

Now, anytime you want to expand the Solution Explorer nodes, just press "alt /". When you want to collapse them all, press "alt \".