views:

577

answers:

2

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?

+8  A: 

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"

Then, reopen the source file to reload outlining.

Samuel_xL
Hmm, well it works, but unfortuatly it is essentaly commenting the entire chunk of code out of my program. I want to be able to collapse working code. This is especialy usefull when i want to add a condition (an if statement) to activate a large chunk of code without putting it into a function (it dosent work as a function, so i dident make it as one).
Faken
If you're using an if statement, then you already have a collapsable block. So I don't get it. Btw, #pragma region keeps the code and enables you to collaspe working code, just as you need I guess.
Samuel_xL
Its more of like putting in new if statements. There are simply too many loops in my program (i think its at around 10 nested loops + a whole bunch of other if statements) and its beocmming hard to keep track of what is inside of what. How do i collapse loops and if statements?
Faken
I'm not sure why you think it is commenting out the code. You should be able to use the #pragma regions statements to create collapsible chunks of working code.
epotter
@Faken, ok now i get it, automatic outlining of statement blocks is not enabled by default in C++. i've edited.
Samuel_xL
Thats it! thank you very much!
Faken
+1  A: 

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.

epotter