Quick code commenting and uncommenting using Visual Studio

Yesterday, I was talking with one of the folks here and I learned something about Visual Studio.  We were looking at some code and he selected a line, clicked a button, on the toolbar, and the line was commented out.  I asked him what he just did and he introduced me to the handy comment / uncomment buttons.  For years, I've been a C/C++/C# multi-line comment person, manually adding /* and */ around any code I wanted to temporarily remove.  While that is a fast / easy way to comment out a block of code, this is even easier, especially when I discover that I already have a multi-line comment block within the code I wish to disable.  For languages which do not support multi-line comments, this saves a lot of time.

To illustrate this feature, I will use the following simple method:
protected override void OnPaint(PaintEventArgs e){    Graphics g = e.Graphics;    Single x = 0f;    Single y = 0f;    String message = ".NET Compact Framework";    SizeF msgSize = g.MeasureString(message, this.Font);    x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) -           (msgSize.Width / 2);    y = (Single)(Screen.PrimaryScreen.Bounds.Height / 2) -           (msgSize.Height / 2);    g.DrawString(message,                  this.Font,                  new SolidBrush(Color.Blue),                 x, y);}

Commenting
The above code centers a string message in a Form's OnPaint method.  Let's comment out the portion that calculates the string position.

  1. Highlight the following lines:
    SizeF msgSize = g.MeasureString(message, this.Font);x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) -       (msgSize.Width / 2);y = (Single)(Screen.PrimaryScreen.Bounds.Height / 2) -       (msgSize.Height / 2);
  2. On the text editor toolbar, click the Comment button (it's the one with the horizontal black and cyan lines)
    Alternately, you can use the keyboard shortcut (Ctrl+K followed by Ctrl+C)

The method now looks like this:
protected override void OnPaint(PaintEventArgs e){    Graphics g = e.Graphics;    Single x = 0f;    Single y = 0f;    String message ".NET Compact Framework";    //SizeF msgSize = g.MeasureString(message, this.Font);    //x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) -     //      (msgSize.Width / 2);    //y = (Single)(Screen.PrimaryScreen.Bounds.Height / 2) -     //      (msgSize.Height / 2);    g.DrawString(message,                  this.Font,                  new SolidBrush(Color.Blue),                 x, y);}

Uncommenting
Now, let's uncomment the code that centers the message horizontally (leaving it at the top of the form).

  1. Highlight the following lines:
    //SizeF msgSize = g.MeasureString(message, this.Font);//x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) - //      (msgSize.Width / 2));
  2. On the text editor toolbar, click the Uncomment button (it's the one with the horizontal black and cyan lines, with a blue reverse arrow)
    Alternately, you can use the keyboard shortcut (Ctrl+K followed by Ctrl+U)

The method now looks like this:
protected override void OnPaint(PaintEventArgs e){    Graphics g = e.Graphics;    Single x = 0f;    Single y = 0f;    String message ".NET Compact Framework";    SizeF msgSize = g.MeasureString(message, this.Font);    x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) -           (msgSize.Width / 2));    //y = (Single)(Screen.PrimaryScreen.Bounds.Height / 2) -     //      (msgSize.Height / 2));    g.DrawString(message,                  this.Font,                  new SolidBrush(Color.Blue),                 x, y);}

Enjoy!
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.