How to create Collapse All macro in C#

.NET programming topics
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to create Collapse All macro in C#

Post by Neo » Thu Dec 24, 2009 1:17 am

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
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Collapse All macro in C#

Post by Neofriend » Thu Dec 24, 2009 1:38 am

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?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Collapse All macro in C#

Post by Neo » Thu Dec 24, 2009 2:03 am

This is a very common code that I got from web long time ago.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Collapse All macro in C#

Post by Neofriend » Thu Dec 24, 2009 2:06 am

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Collapse All macro in C#

Post by Neo » Thu Dec 24, 2009 2:09 am

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 :)
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Collapse All macro in C#

Post by Neofriend » Thu Dec 24, 2009 2:12 am

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Collapse All macro in C#

Post by Neo » Thu Dec 24, 2009 2:25 am

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.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Collapse All macro in C#

Post by Neofriend » Thu Dec 24, 2009 2:28 am

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to create Collapse All macro in C#

Post by Neo » Thu Dec 24, 2009 2:34 am

Try to rename Module1 to CollapseAll
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to create Collapse All macro in C#

Post by Neofriend » Thu Dec 24, 2009 2:39 am

Changed, and its all the same. :(
Post Reply

Return to “.NET Programming”