25

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?

3 Answers 3

48

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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).
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?
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.
6

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.

Comments

0

This extension is made for the job in Visual Studio: http://visualstudiogallery.msdn.microsoft.com/4d7e74d7-3d71-4ee5-9ac8-04b76e411ea8

1 Comment

@Amjad - the developer states in the Q&A that its a quick fix in the source to work with C++/C code. You will just need to change language affinity attribute evidently. I only use VisStudio for C#, Javascript and PHP so haven't tried it. Link to source code of the extension is here: github.com/Skybladev2/C--outline-for-Visual-Studio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.