Expand and Collapse Code Blocks on the fly in Visual Studio

By | February 13, 2016

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..


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.


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

Hope this helps !