MrDave's (David Yack) WebLog

VS Macro / Shortcuts to Expand / Collapse all regions

Using regions in code to break up and make it easy to look at seems to becoming more popular.

I have to admit I have a love / hate relationship with regions because sometimes they just get in the way.

Ran across a post from Roland Weigelt where he has a macro that helps expand / collapse all the regions in a source file.

Here’s the link to the post Better Keyboard Support for #region ... #endregion

Roy Osherove also provided the following key strokes that are built into Visual Studio:

CTRL+M CTRL+M (that's two key presses!) - collapse/open the current parent region

CTRL+M CTRL+L - Collapse/Open all regions in document recursively (meaning you might get only one line in the document - one big namespace region which is collapsed or you'll see the entire page code uncollapsed

CTRL+M CTRL+O - Collapse all regions not recursively -

Thanks Roland and Roy!

 

Comments

Mark Heimonen said:

Thanks for the insight on regions. I feel like somebody broke the way regions function between VS.NET 2002 and VS.NET 2003. In VS.NET 2002, I used regions extensively, and had some ASP.NET pages with an extensive number of regions.

Unless I'm mistaken, the "Outlining-->Toggle All Outlining" used to automatically expand all the regions in the document. Since this no longer works (It only expands the namespace), I have completely abandoned the use of regions. To make matters worse, the "Find in Document" feature doesn't work if the regions are collapsed, which means I have to manually expand every region before I can perform the find command. I'd like to try out the Macro you mentioned to see if it fixes this issue. Hopefully, they have this all straightened out with Whidbey.
# September 17, 2004 2:54 PM

Dry said:

'Installation Instructions:

'

'(NOTE: I have VS 2005 Professional, other IDE's may differ)

'

'Open Macro IDE (ALT+F11)

'Create new Module

'Paste Code Below inside Module

'Close and Return (ALT-Q)

'Right-Click on VS IDE Toolbar - Customize

'Click Keyboard Button

'In textbox "Show commands containing:" enter 'Macros'

'Select ExpandAllRegions

'In textbox "Press Shortcut keys" Press CTRL and Numpad+

'Click Assign

'Select CollapseAllRegions

'In textbox "Press Shortcut keys" Press CTRL and Numpad-

'Click Assign

'Then Ok out of the dialogs, and open a code window with regions test your new keys

   ' Expands all regions in the current document

   Sub ExpandAllRegions()

       DTE.SuppressUI = True

       Dim objSelection As TextSelection

       objSelection = DTE.ActiveDocument.Selection

       objSelection.StartOfDocument()

       Do While objSelection.FindText("#Region", vsFindOptions.vsFindOptionsMatchInHiddenText)

           objSelection.WordRight()

           'System.Threading.Thread.Sleep(0.5)

       Loop

       DTE.ActiveDocument.Selection().StartOfDocument()

       DTE.SuppressUI = False

   End Sub

   ' Collapses all regions in the current document

   Sub CollapseAllRegions()

       ExpandAllRegions()

       Dim objSelection As TextSelection

       objSelection = DTE.ActiveDocument.Selection

       objSelection.EndOfDocument() ' Lets close them in reverse order (for nested Regions)

       Do While objSelection.FindText("#Region", vsFindOptions.vsFindOptionsBackwards)

           objSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

           DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")

           objSelection.EndOfDocument()

       Loop

       DTE.ActiveDocument.Selection.StartOfDocument()

   End Sub

# August 30, 2007 5:20 PM

LADY.LOVE » Visual Studio .NET 2003 Shortcuts said:

Pingback from  LADY.LOVE » Visual Studio .NET 2003 Shortcuts

# October 10, 2007 2:03 AM

THIS WAS THE GREATEST POST EVER said:

Ive been trying to figure out these hot keys for god only knows how long. I <3 you guys. Amazing.

# September 26, 2008 3:05 PM

PJ said:

Thanks for this it was bugging the hell out of me!

BTW.  Personally I find running CTRL+O followed by the ExpandAllRegions macro extremely useful to get an overview of the module.

# November 7, 2008 10:09 AM

Ashok said:

You can find the text hidden in a collapsed region. While finding, the find dialog has an option called search hidden text under the Find options

HTH

# December 9, 2008 1:30 AM

Ed said:

Thanks for the Macro - very cool - will be using all the time :-)

# June 19, 2009 10:47 AM

ffortino said:

Thanks a lot

I've been avoiding Regions, now I can use them again

Great job!

Thanks, again

# June 20, 2009 10:23 PM

Roger said:

Thanks for the Tip !!!

# August 13, 2009 3:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)