Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I collapse selected chunks of code in Visual Studio 2008?

Tags:

In Visual Studio 2008: Is there a way for me to customly collapse bits of code similar to like how I can automatically collapse chunks of comments?

like image 927
Faken Avatar asked Oct 26 '09 17:10

Faken


People also ask

How do you collapse all code blocks in Visual Studio?

CTRL + M + O will collapse all.

How do I hide part of a code?

To collapse and hide a section of codeThe #Region block can be used multiple times in a code file; thus, users can define their own blocks of procedures and classes that can, in turn, be collapsed. #Region blocks can also be nested within other #Region blocks.


2 Answers

Your piece of code needs to be a block surrounded by, as desired:

  • braces
  • #region and #endregion in C#
  • #pragma region and #pragma endregion in C/C++

If you can't collapse statement blocks, you need to enable this feature :

Tools -> Options -> Text Editor -> C/C++ -> Formatting -> check everything in "outlining"

(In Visual Studio 2013 it's Tools -> Options -> Text Editor -> C/C++ -> View)

Then, reopen the source file to reload outlining.

like image 185
KeatsPeeks Avatar answered Sep 18 '22 12:09

KeatsPeeks


TheSam is right, you can create collapsible chunks with the #pragma region and #pragma endregion statements.

Here is a sample:

int main(array<System::String> args) {       Console::WriteLine(L"This");     Console::WriteLine(L"is");     Console::WriteLine(L"a");     #pragma region     Console::WriteLine(L"pragma");     Console::WriteLine(L"region");     #pragma endregion      Console::WriteLine(L"test.");     return 0; } 

In the above sample, everything between the samples can be collapsed.

You can also specify what text is displayed when it is collapsed. You can do that like this:

#pragma region The displayed text 

That would obviously display "The displayed text" when the region was collapsed.

like image 42
epotter Avatar answered Sep 19 '22 12:09

epotter