Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

How to: Reformat your code in Visual Studio

We're all really fast at coding, right. So you don't want to bother with things like proper casing of keywords (in VB.Net) and properly re-aligning the code. Well, Visual Studio can do that for you with just a few keystrokes. I noticed that not that many people are aware of this option, but I actually use it quite often. Especially when looking at existing code, I use this key-combination to make sure the code is properly aligned. So let me explain how this key-combination works and what it can and cannot do.

Reformat VB.Net code

Suppose I have the VB.Net as it is shown here:

private _myPropertyValue as String
 
public property PropertyValue as string
    get
        return _myPropertyValue
    End Get
    set 
        _myPropertyValue = value
    End Set
End Property

This code will compile and it will work. However, you will also notice that the casing of keywords is not as you would expect as a VB.Net developer. To re-format the code, follow these three steps:

  1. Select the code you want to re-format.
  2. Press Ctrl-K
  3. Press Ctrl-F

The code should now look like this:

Private _myPropertyValue As String
 
Public Property PropertyValue() As String
    Get
        Return _myPropertyValue
    End Get
    Set(ByVal Value As String)
        _myPropertyValue = Value
    End Set
End Property

As you will see, all VB.Net keywords are now properly cased. But you also get the (ByVal Value As String) code after the Set keyword, all for free. The key combination Ctrl-K + Ctrl-F only works on selected text, so make sure you select the code you want to re-format before using it. You can easily reformat the entire code by pressing Ctrl-A + Ctrl-K + Ctrl-F.

Reformat C# code

The key combination also works in C#. But the functionality in C# is really only useful to re-align the code. Look at the following example:

internal class temp {
    private string _mypropertyvalue;
 
    public string PropertyValue {
        get {
            return _mypropertyvalue; 
        }
        set { 
            _mypropertyvalue = value; 
        }
    }
}

When you use the Ctrl-K + Ctrl-F combination to reformat the code, it will look like this:

internal class temp 
{
    private string _mypropertyvalue;
 
    public string PropertyValue 
    {
        get 
        {
            return _mypropertyvalue; 
        }
        set 
        { 
            _mypropertyvalue = value; 
        }
    }
}

As you will undoubtedly notice, the code is merely re-aligned. But I still found it useful in C#, because I don't have to worry about re-aligning my code. I just use this key-combination to do it for me. 

Comments

ShoddyCoder said:

Hey cool, I knew there was a combination to do that, and I was recently looking for it for C# code... that is my biggest pet peeve in C# is that the editor experience isn't as good as in VB for formatting... my 2nd pet peeve while I'm on the subject is when you type a using statement at the top the intellisense menu (which overall is "better" than vb) doesn't pop down. I'm assuming you got your VB to look like that by running it through a converter... Thanks for the tip :D
# November 15, 2006 7:47 AM

Richthofen said:

I think the combo to do this without selecting the text is Ctrl-K Ctrl-D

# March 29, 2007 9:26 AM

Bane said:

Just what I was looking for.. Thanks!!

# January 29, 2009 6:55 PM

anony said:

thanks i needed this

# February 25, 2009 8:10 AM

Chris said:

Very nice, but is there any way to do something like this for C++ code in Visual Studio?

# April 16, 2009 3:13 PM

Jan Schreuder said:

I don't know, but you could try to use this solution in C++. Perhaps it works in VS 2005 and up. I know that XML and ASPX can be formatted in these versions using the same trick as the C# code.

# April 16, 2009 4:24 PM

Pickle Pumpers said:

You can also do this:

Select All: [CTRL] + A

Cut: [CTRL] + X

Paste: [CTRL] + Y

# May 12, 2009 6:03 PM

mohammad said:

thanks...

# June 8, 2009 1:44 AM

Yumi Nanako said:

Heh, thanks. It's a really useful tip.. Mostly I use it to re-align my code in ASP.NET where my HTML tags really get messy.

# August 26, 2009 5:13 PM

maansi said:

Thanks Very Much I was a bit bit rusty about it but thanks a lot it has helped

# November 11, 2009 11:45 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 7 and type the answer here: