Expand and Collapse Code Blocks on the fly in Visual Studio

Expand and Collapse Code Blocks on the fly in Visual Studio

We all are very much familiar with #region in Visual Studio that let us specify a block of code to categorized and allow to expand and collapse.  With having a region name given, it is often easier to segregate the code module and keep on the code block which is required. While you can have defined regions for different code block, often for a larger files you many need to focus on specific set of code and you don’t want to have separate region name for them. In that case, you can just simply collapse them on the fly and use them as long as you need them.

Here is the steps to follow:

  • Select the section of code
  • Press Ctrl+M+H –  Turn on the collapsible/expandable option
  • Press Ctrl+M+UTurn off the the collapsible/expandable option.

 

Related Tip: Surround generated code with #region while implement Interface

 

  • Here is how it looks like..

image

To remove the option you really don’t need to select the section back again. You can just place the cursor anywhere in the defined block and press the Ctrl+M+U option to remove the options.

 

Related Tip :  How to select Block of Code in Visual Studio?

 

This works well as similar to other collapsible panel in Visual Studio code editor. You can just move hover on it to get a preview of the content.

image

This is really interesting feature and can make your code navigation very easy and faster.

Hope this helps !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.