 |
|
 |
Thank you for posting this. It has been a big help.
|
|
|
|
 |
|
 |
please reference:
http://www.usysware.com/DPack/Default.aspx
Right click the solution, there's a menu item "Collapse all projects".
in VS2005, there's also a short cut mapped to this: SHIFT + ALT + C
DPack do a boundle of wonderful thing to improve developers' productvity, worth a try.
But, DPack's solution in vs2003 looks very slow compared to your macro's way.
I think it's better to use the macro for real large .sln file.
|
|
|
|
 |
|
 |
There is a bug in VS 2005 that keeps some nodes from collapsing. The fix and a code enhancement to collapse all sub-nodes can be found here: http://geekswithblogs.net/scottkuhl/archive/2007/04/09/111195.aspx
|
|
|
|
 |
|
 |
I have made some improvements to Scott's code. My code can collapse selected item and runs faster than Scott's code on a very large solution.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module Collapse
Sub CollapseItem()
' 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
'Find selected node
Dim selectedNode As UIHierarchyItem = FindSelectedNode(rootNode, solutionExplorer)
' Collapse each node
Collapse(selectedNode, solutionExplorer)
' Select the selceted node
selectedNode.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
If innerItem.UIHierarchyItems.Expanded Then
' Re-cursive call
Collapse(innerItem, solutionExplorer)
End If
' 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
Private Function FindSelectedNode(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy) As UIHierarchyItem
If item.IsSelected Then
Return item
End If
For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then
If innerItem.UIHierarchyItems.Expanded Then
' Re-cursive call
Dim result As UIHierarchyItem
result = FindSelectedNode(innerItem, solutionExplorer)
If Not result Is Nothing Then
Return result
End If
End If
If innerItem.IsSelected Then
Return innerItem
End If
End If
Next
Return Nothing
End Function
End Module
|
|
|
|
 |
|
 |
A simple but very useful utility, thanks!
|
|
|
|
 |
|
 |
I sometimes need to collapse more than just the top level. Here's some code to configure the depth.
Sub CollapseTop()
Collapse(3)
End Sub
Sub CollapseAll()
Collapse(-1)
End Sub
Sub Collapse(ByVal maxDepth As Integer)
' 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)
' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
CollapseLevel(UIHItem, maxDepth)
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)
End Sub
Sub CollapseLevel(ByVal node As UIHierarchyItem, ByVal maxDepth As Integer, Optional ByVal currentLevel As Integer = 0)
'would like to be able to determine if this was a folder. But didn't see the option on the object.
If (maxDepth = -1 Or currentLevel < maxDepth) Then
currentLevel = currentLevel + 1
node.UIHierarchyItems.Expanded = False
Dim UIHItem As UIHierarchyItem
For Each UIHItem In node.UIHierarchyItems
CollapseLevel(UIHItem, maxDepth, currentLevel)
Next
End If
End Sub
-- modified at 17:36 Tuesday 14th February, 2006
|
|
|
|
 |
|
 |
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)
CollapseRec(UIHSolutionRootNode)
' Collapse each project node
' 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)
End Sub
Sub CollapseRec(ByVal UIHParent As UIHierarchyItem)
Dim UIHItem As UIHierarchyItem
If UIHParent Is Nothing Then
Return
End If
' Check if there is any open solution
If (UIHParent.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
For Each UIHItem In UIHParent.UIHierarchyItems
CollapseRec(UIHItem)
UIHItem.UIHierarchyItems.Expanded = False
Next
End Sub
Bnaya Eshet
Chief Architect at
Wise Mobility
www.wisemobility.com
|
|
|
|
 |
|
 |
Great makro. Works in VS 2003 as well.
M
|
|
|
|
 |
|
 |
while i am not a total fan of having the solution explorer follow the open code, i find i need to keep it turned on since it causes more problems turning it off.
as a result a rather large tree ends up open all over the place, making a complete mess. this is ideal
zen is the art of being at one with the two'ness
|
|
|
|
 |
|
 |
I modified the macro slightly so it will collapse all subfolders in the solution tree as well...
'------------------------------------------------------------------------------
Sub CollapseMe(oRootItem As UIHierarchyItem)
Dim oChildItem As UIHierarchyItem
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
Next
oRootItem.UIHierarchyItems.Expanded = False
End Sub
'------------------------------------------------------------------------------
Sub CollapseAll()
'DESCRIPTION: Colapse all the nodes in the project tree
' Get the the Solution Explorer tree
Dim oSolutionExplorer As UIHierarchy
oSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (oSolutionExplorer.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 oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem
' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem)
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.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Charles
|
|
|
|
 |
|
 |
Hi Edwin,
Have you tried applying your macro to ClassView? For some reason doing this
DTE.Windows.Item(Constants.vsext_wk_ClassView).Object()
always gives you Nothing.
I've also tried:
DTE.Windows.Item(Constants.vsWindowKindClassView).Object()
Same result.
Referencing the ClassView window is OK though. It's only when you apply .Object that you get Nothing.
Kevin
Kevin
|
|
|
|
 |
|
 |
Apparently the classview does not implement anything at all to be able to access the tree ,
but here is a trick that could help you out :
There are commands to switch the type of treeview within the classview :
1. View.ClassViewGroupByType
2. View.SortAlphabetically
3. View.SortByAccess
4. View.SortByType
If you call 1. and 4. using
dte.ExecuteCommand("View.ClassViewGroupByType", "");
dte.ExecuteCommand("View.ClassViewSortByType", "");
The tree is collapsed , sadly enough i did not find a way yet to test which one is
active so it could be that you end up with enother sort then you started off with
Brgds,
Erlend
Erlend Robaye
Fluxys N.V.
Belgium
Erland.Robaye@fluxys.net
|
|
|
|
 |
|
 |
Great macro.
How did you add the ToolButton which says "Collapse All" in the Solution Explorer's toolbar?
Thanks.
PKP
|
|
|
|
 |
|
 |
Here is a explanation of how to add a button to the toolbar: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q318651#4. Start with step 6.
If you want to do this programmatically, here is what you could do: (I wrote an Add-In version of this same feature in C#)
Command command = commands.AddNamedCommand(addInInstance, "CollapseAll", "CollapseAll", "Collapses all the project nodes in the Solution Explorer", true,
41, // blue arrow, see http://www.j-walk.com/ss/excel/tips/tip40.htm, thanks to John Walkenbach
ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
// This will add the button to the end of the standard toolbar
// but the user always has the ability to customize this.
CommandBar commandBar = (CommandBar)commandBars["Standard"];
int nPositionEnd = commandBar.Controls.Count + 1;
CommandBarControl commandBarControl = command.AddControl(commandBar, nPositionEnd);
-Edwin
|
|
|
|
 |
|
 |
Hi Edwin,
Thanks for the reply.
I knew how to add a CommandBarControl to a toolbar (via code or using Tools|Customize. However in your screenshot you had added a "CollapseAll" button to a toolbar which seems like part of "Solution Explorer". That toolbar does not seem to be customizable using Tools|Customize.
So how did you do it? I will be incorporating this in an add-in.
You did mention how can I add a toolbutton to the "Standard" toolbar. But how can I add it to a Solution Explorer toolbar? Is there a string name for that toolbar?
I will appreciate your help in this regard.
Thanks.
Paresh
|
|
|
|
 |
|
 |
If you look at the pictures again, you'll see that Edwin's button is *not* inside the Solution Explorer.
|
|
|
|
 |
|
 |
This is a great macro, but I wouldn't call the auto-sync feature in the Solution Explorer a "great feature" .
Thanks,
- Stephen Jones
|
|
|
|
 |
|
|
 |
|
 |
It was great, but not quite what I was looking for. Here's my modifications, which allow the macro to collapse all subfolders of expanded folders as well. It does not deal with expanded subfolders in collapsed folders, as that takes much too long (it actually seemed to hang my VS).
Enjoy!
Sub CollapseNode(ByRef item As UIHierarchyItem)
Dim subitem As UIHierarchyItem
For Each subitem In item.UIHierarchyItems
If (subitem.UIHierarchyItems.Expanded = True) Then
CollapseNode(subitem)
subitem.UIHierarchyItems.Expanded = False
End If
Next
End Sub
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)
CollapseNode(UIHSolutionRootNode)
' 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)
End Sub
|
|
|
|
 |
|
 |
Hey, thanks Booga! That's just what I need with the new project I'm working on.
-Edwin
|
|
|
|
 |
|
 |
Thanks, exactly what I needed.
|
|
|
|
 |