Jay Harris's blog on .net development, automation, and improving quality through code. RSS 2.0
# Wednesday, May 31, 2006

Most of our VS.Net solutions contain hundreds of files (classes) organized neatly into dozens of folders (namespaces), but despite all of this organization the vertical content size of the Solution Explorer can get quite large. Finding a particular file when the majority of the tree is expanded is tedious and time-consuming, considering it should be a simple effort of less than five seconds. Fortunately, all of this is solved by the click of a button (assigned to handy macro).

The most useful macro for Visual Studio that I have ever encountered (and in the running for most useful VS tool, period) is the CollapseAll macro authored by one current and one former colleague, Dennis Burton and Mike Shields. In a quick XP effort, Dennis and Mike created a handy macro that recursively collapses the entire Solution Explorer tree down to just the solution and its projects.

With the tree collapsed, it is easy to find that desired file.

CollapseAll Macro for Microsoft Visual Studio.Net 2003
Dennis Burton & Mike Shields | Published with Permission

Imports EnvDTE

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)

        UIHSolutionRootNode.DTE.SuppressUI = True

        ‘ Collapse each project node

        Dim UIHItem As UIHierarchyItem

        For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems

            ‘UIHItem.UIHierarchyItems.Expanded = False

            If UIHItem.UIHierarchyItems.Expanded Then

                Collapse(UIHItem)

            End If

        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)

        UIHSolutionRootNode.DTE.SuppressUI = False

    End Sub

 

    Private Sub Collapse(ByVal item As UIHierarchyItem)

        For Each eitem As UIHierarchyItem In item.UIHierarchyItems

            If eitem.UIHierarchyItems.Expanded AndAlso eitem.UIHierarchyItems.Count > 0 Then

                Collapse(eitem)

            End If

        Next

        item.UIHierarchyItems.Expanded = False

    End Sub

 

End Module

Based on code from Edwin Evans

Here, the macro is so popular that it is a part of our default developer’s build for every new machine, and is conveniently assigned to a toolbar button. The default button icon list contains an Up Arrow (in the Change Button Image menu when customizing the toolbar) that seems quite appropriate. That little button has saved us all from a lot of pain, five seconds at a time.

Wednesday, May 31, 2006 9:19:55 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Programming | Tools
Navigation
Twitter : Do you follow me?
View Jason Harris's profile on LinkedIn
Upcoming Conferences
Join me at CodeStock
devLink : I'll be there, how about you?
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Jason Harris
Sign In
Statistics
Total Posts: 70
This Year: 9
This Month: 1
This Week: 1
Comments: 1
All Content © 2008, Jason Harris
DasBlog theme 'Business' created by Christoph De Baene (delarou)
Technorati Profile