Page 1 of 2

How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 1:17 am
by Neo
This will recursively collapses the entire Solution Explorer tree down to just the solution and its projects.
  1. Open the Macros IDE (Tools - Macros - Macros IDE)
  2. Create a new module, name it to CollapseAll, replace the code in the created file with the posted source code, save file
  3. Assign keys in VS.Net (Tools - Options - Keyboard)

Code: Select all

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module CollapseAll

    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)

        ' 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 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
End Module

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 1:38 am
by Neofriend
1. Open the Macros IDE (Tools - Macros - Macros IDE)
2. Create a new module, replace the code in the created file with the posted source code, save file
3. Assign keys in VS.Net (Tools - Options - Keyboard)

Above were the steps I just tried, 1 and 2 were easy. In 3, I assigned Ctrl + C, Ctrl + A as shortcut.

Now what is the use of it precisely? and How can I see it in effect? - I tried pressing the assigned keys in coding mode, but nothing seems to show the change.

Plus, I haven't yet read the code, did you write this? - Is it easy to understand or a great deal of logic there?

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:03 am
by Neo
This is a very common code that I got from web long time ago.

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:06 am
by Neofriend
uhun, sounds great.

If you can possibly answer the other confusions quickly?, in short.
I'll be able to try it out the rest. Thanks.

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:09 am
by Neo
When you run it, it should collapse all nodes in the solution explorer.
I didn't try this with VS.Net 2005. However I assume it should work :)

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:12 am
by Neofriend
Am using Windows 7 and VS 2008 - And I don't think am doing something wrong as steps are easy to follow - but it isn't working - so that does make me think may be am missing out something.

Tried 4-5 times, but no luck. I'll see it once again later and let you know.

Thanks though, it helped.

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:25 am
by Neo
I have given it a try and the original post is edited with the new code.
Try it and let me know.
(I had to re-assign the keyboard shortcut after editing)

Expand the tree in your solution explorer window to see what happens.

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:28 am
by Neofriend
No luck so far, tried with the new code too.

Am creating a new module namely Module1, pasting the source code, saving it, assigning the shortcut, closing the window and checking from the main VS window, but it doesn't work.

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:34 am
by Neo
Try to rename Module1 to CollapseAll

Re: How to create Collapse All macro in C#

Posted: Thu Dec 24, 2009 2:39 am
by Neofriend
Changed, and its all the same. :(