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.
- 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);
- 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).
- Highlight the following lines:
//SizeF msgSize = g.MeasureString(message, this.Font);//x = (Single)(Screen.PrimaryScreen.Bounds.Width / 2) - // (msgSize.Width / 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.
Comments
- Anonymous
September 09, 2005
Every now and then I learn a control-k, control-??? shortcut that just blows my mind... and this was one of them. Now to find a list of all such shortcuts to save my time in future of going to the toolbar for common function. - Anonymous
September 09, 2005
This is classic! I'm sure the Ctrl+K, Ctrl+C/U is my most used key chord. That and Ctrl+K, Ctrl+K (toggle bookmarks). - Anonymous
September 09, 2005
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxurfkeyboardenvironmentoptionsdialogbox.asp
And my favorite is Ctrl+I (incremental search) - Anonymous
September 14, 2005
One of my favorites is Ctrl-M, Ctrl-O to collapse outlining code and Ctrl-M Ctrl-M to expand a single outlining. Also try selecting some code then using Ctrl-K Ctrl-F to format the code with the appropriate tabs etc.