Pages

Thursday, May 03, 2012

Collapse All and Expand All Macro

Hi all,
Today i really get irritated as i had to collapse all my expanded projects in my solution in Visual Studio.
So thought share some piece of macro that i used so that you can get benifitted out of it also

Procedure:
1.  goto Tools --> Macros --> Macro Explorer
2. RightClick on My Macro and select New Module...
3. Provide name "CollapseAll"
4. Double click the module and replece the following code in the collapseall file
'-------------------------------------------------------------------------------------------------------------------------------------

Imports

System
Imports

EnvDTE
Imports

EnvDTE80
Imports

System.Diagnostics
Public

Module CollapseAll


Sub CollapseAll()


' Get the the Solution Explorer tree


Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()


' Check if there is any open solution


If (solutionExplorer.UIHierarchyItems.Count = 0) Then


Return


End If


' Get the top node (the name of the solution)


Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI =

True


' Collapse each project node
Collapse(rootNode, solutionExplorer)


' 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.
rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI =

False


End Sub


Sub CollapseSelected()


' Get the the Solution Explorer tree


Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()


' Check if there is any open solution


If (solutionExplorer.UIHierarchyItems.Count = 0) Then


Return


End If


' Get the top node (the name of the solution)


Dim selected As Array = solutionExplorer.SelectedItems


If (selected.Length = 0) Then Return


Dim rootNode As UIHierarchyItem = selected(0)
rootNode.DTE.SuppressUI =

True


' Collapse each project node
Collapse(rootNode, solutionExplorer)


' 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.
rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI =

False


End Sub


Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)


For Each innerItem As UIHierarchyItem In item.UIHierarchyItems


If innerItem.UIHierarchyItems.Count > 0 Then


' Re-cursive call
Collapse(innerItem, solutionExplorer)


' Collapse


If innerItem.UIHierarchyItems.Expanded Then
innerItem.UIHierarchyItems.Expanded =

False


If innerItem.UIHierarchyItems.Expanded = True Then


' Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()


End If


End If


End If


Next


End Sub
End
Module



'-------------------------------------------------------------------------------------------------------------------------------------
5. Save the file and close the explorer
6. Open Tools --> Customize --> RearrangeCommand
7. Choose the "ToolBar" radio button
8. Click the "Add" button and choose "New Menu" from the dialogue box
9. Click "Modify Selection" and change the name to "MyMenu"
10. Now choose radio button "Menu Bar" and select "MyMenu" from there.

11. Click "Add" button
12. From the dialogue box choose "Macros" from left side and choose "Macros.MyMacros.CollapseAll.CollapseAll". Click OK
13. Click "Modify Selection" and change the Name to "CollapseAll"
14. Close the box and click "KeyBoard" button

15. in Show Commands Containing textbox write "Macros.MyMacros.CollapseAll.CollapseAll" and select it in the list
16. in the press shortcut key press "CTRL+C  CTRL+A" and assign it
17. Click "Ok" button


Check that your visual studio has a new menu "MyMenu" and it has a child menu "CollapseAll".
If you click it all your projects will get collapsed.
You can also use CTRL+C CTRL+A to do the same thing



You can also include a ExpandAll macro to your visualstudio
Follow the same steps for Expand All

here is the code

-------------------------------------------------------------------------------------------------------------------------------------------------------
SystemImports EnvDTEImports EnvDTE80Imports System.DiagnosticsPublic

Module ExpandAllSub ExpandAll()' Get the the Solution Explorer tree
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
Dim solutionExplorer As UIHierarchy' Check if there is any open solutionIf (solutionExplorer.UIHierarchyItems.Count = 0) ThenReturnEnd If' Get the top node (the name of the solution)
rootNode.DTE.SuppressUI =
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)True' Expand each project nodeExpand(rootNode, solutionExplorer)
' 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.rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI =
FalseEnd Sub
Sub ExpandSelected()' Get the the Solution Explorer tree
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
Dim solutionExplorer As UIHierarchy' Check if there is any open solutionIf (solutionExplorer.UIHierarchyItems.Count = 0) ThenReturnEnd If' Get the top node (the name of the solution)
Dim selected As Array = solutionExplorer.SelectedItemsIf (selected.Length = 0) Then Return
rootNode.DTE.SuppressUI =
Dim rootNode As UIHierarchyItem = selected(0)True' Expand each project nodeExpand(rootNode, solutionExplorer)
' 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.rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI =
FalseEnd Sub

Private Sub Expand(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)For Each innerItem As UIHierarchyItem In item.UIHierarchyItemsIf innerItem.UIHierarchyItems.Count > 0 Then' Re-cursive callExpand(innerItem, solutionExplorer)
' ExpandIf innerItem.UIHierarchyItems.Expanded = False TheninnerItem.UIHierarchyItems.Expanded = TrueIf innerItem.UIHierarchyItems.Expanded = False Then' Bug in VS 2005innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()
End IfEnd IfEnd IfNext
End
End Sub Module

-------------------------------------------------------------------------------------------------------------------------------------------------------

Please revert with comments
Imports

No comments:

Post a Comment