locked
How can I indent and un-indent a block of code in Visual C# code editor?

    Question

  • I'm using Microsoft Visual C# 20010 Express for creating Windows Forms Application and Windows Games with XNA Game Studio. I'm quite new to the keyboard shortcuts (eg: Ctrl+E, C for commenting a selection) and am practising them these days. those are very helpful. I sometimes use the 'Surround With' tool to move a block of code in an if block. Doing this indents the selected code block one tab and adds curly braces around them and adds an if statement to the top as well. But when I see that I don't need the if block any more, I've to clear the if statement (eg: if (cmbBox1.SelectedIndex == 1)) and the curly braces as well. But when I do that, the code block inside the curly braces doesn't come back to their original position.

    Is there any keyboard shortcut that indents and un-indents selected code one tab? Or any other tool?

    Saturday, January 28, 2012 11:53 AM

Answers

  • Hi ddlyProgrammer,

    Visual studio’s smart indenting does a pretty good job of automatically indenting code as it is written or pasted into the code editor.

    When writing code, to indent an entire block of code one level without changing each line individually, simply select the block and press "Tab". To unindent a block one level , select it and press " Shift+Tab ".

    In addition, if we want to have VS reformat the entire document and set the brace locations and line indentations, select Edit->Advanced->Format Document or press “Ctrl+K, Ctrl+D”. If to reformat just the selected code block, select Edit->Advanced->Format Selection or press” Ctrl+K, Ctrl+F”.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us


    Monday, January 30, 2012 8:49 AM

All replies

  • Hi ddlyProgrammer,

    Visual studio’s smart indenting does a pretty good job of automatically indenting code as it is written or pasted into the code editor.

    When writing code, to indent an entire block of code one level without changing each line individually, simply select the block and press "Tab". To unindent a block one level , select it and press " Shift+Tab ".

    In addition, if we want to have VS reformat the entire document and set the brace locations and line indentations, select Edit->Advanced->Format Document or press “Ctrl+K, Ctrl+D”. If to reformat just the selected code block, select Edit->Advanced->Format Selection or press” Ctrl+K, Ctrl+F”.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us


    Monday, January 30, 2012 8:49 AM
  • Thanks a lot for the help. At first, I didn't find the Advanced submenu in the View menu but the shortcut worked. Later I found that I've to select Tools->Settings->Expert Settings rather than Basic Settings to get this submenu in the View menu. Doing that also enabled many other useful features in the menus.
    Monday, January 30, 2012 10:33 AM