11 Replies - 1840 Views - Last Post: 09 December 2010 - 06:28 AM

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

How to format code before posting

Posted 10 November 2010 - 08:45 AM

DIC is the best programming forum on the web for students and professionals. If you have a question you'd like someone to clear up for you, please take the time to properly present your question.

If you don't take 2 seconds to format your code, why should a stranger take 5 minutes helping someone out?

Here's how to format your code:

Visual Studio 2010 (works on all versions)

In the menu, under the Edit > Advanced > Format Document.

Or you can press Ctrl+E, D.

Or you can press Ctrl+K, D.

Or you can press Ctrl+K, F. (This only formats the selected code)


Thanks for taking the extra effort! Thanks for helping us help you.

This post has been edited by Sergio Tapia: 10 November 2010 - 09:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to format code before posting

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: How to format code before posting

Posted 10 November 2010 - 09:06 AM

Oddly control+K, control+D also formats the entire document. To me this goes better with their shortcut of cntrl+K, contrl+F to format the selected lines

Klean Document
Klean From selected

This post has been edited by tlhIn'toq: 10 November 2010 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to format code before posting

Posted 10 November 2010 - 09:14 AM

Thanks, adding it.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to format code before posting

Posted 10 November 2010 - 11:42 AM

The problem is, nobody is going to see this in 2 days, and new members aren't going to read threads. I am going to move it to the .Net Framework forum.
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to format code before posting

Posted 10 November 2010 - 01:07 PM

Why not sticky it up top the C# forum just like the other Java thread?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: How to format code before posting

Posted 10 November 2010 - 01:40 PM

We tried to clean up the massive amounts of stickies months ago. Fact is, we're lucky if we can get people to even use CODE TAGS, never mind format the code properly. It would just be another sticky for people to ignore. It's the law of diminishing returns.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to format code before posting

Posted 10 November 2010 - 02:24 PM

I agree with JackOfAllTrades. We can barely get members to use the CODE TAGS when it's written in a big pink box at the top of the page. New members rarely read the stickied topics.

Also, this is really for all code in .Net, not just C#. That's the main reason I moved it here.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: How to format code before posting

Posted 16 November 2010 - 10:12 AM

Another tip I use:

Hold ALT when selecting a block of text. It switches the selection mode from standard to block.

Example:
Posted Image

Of course, that's a bad example, because why would I want to select that garbage?

Here's a better one:
Posted Image

Here's the pasted result of that selection:
/// <summary>
/// Attempts to bind an ObjectStructure object to the TreeView.
/// </summary>
/// <param name="os">The ObjectStructure to bind.</param>
private void BindObjectStructureToTree(ObjectStructure os, TreeViewItem requestRoot)
{
    Log.LogDebug("Entering method.");
    //set the header of the default element
    requestRoot.Header = os.Name;
    //assume that the OS is a class.  Process each child node.
    foreach (var c in os.Children)
    {
        ProcessNode(c, requestRoot, os.Name);
    }
    requestRoot.IsExpanded = true;
    Log.LogDebug("Exit method.");
}




Compare that to this:
Posted Image
/// <summary>
        /// Attempts to bind an ObjectStructure object to the TreeView.
        /// </summary>
        /// <param name="os">The ObjectStructure to bind.</param>
        private void BindObjectStructureToTree(ObjectStructure os, TreeViewItem requestRoot)
        {
            Log.LogDebug("Entering method.");
            //set the header of the default element
            requestRoot.Header = os.Name;
            //assume that the OS is a class.  Process each child node.
            foreach (var c in os.Children)
            {
                ProcessNode(c, requestRoot, os.Name);
            }
            requestRoot.IsExpanded = true;
            Log.LogDebug("Exit method.");
        }



The standard selection copies all the whitespace. You could fix this garbage by selecting the whitespace in front of the first line, but then the whole thing is indented. Or you could select it, then SHIFT-TAB until there's no more whitespace, but then you have to TAB it back. I like the block select.
Was This Post Helpful? 2
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to format code before posting

Posted 16 November 2010 - 04:17 PM

I think block select was added to VS2010, so those using VS2005 and VS2008 would not have that available to them. I use the SHIFT-TAB technique.
Was This Post Helpful? 0
  • +
  • -

#10 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to format code before posting

Posted 19 November 2010 - 12:07 PM

i use 2008 and i can use block select
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to format code before posting

Posted 03 December 2010 - 03:59 PM

View PostinsertAlias, on 16 November 2010 - 12:12 PM, said:

Most useful tip in a long time.


I wish I could upvote you more than once! This is a great tip! This needs to be publicized much MUCH more!
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to format code before posting

Posted 09 December 2010 - 06:28 AM

I am surprised I didn't get a downvote for my previous post. Seems that block select is actually available in VS2005, 2008, and 2010. My confusion came from the enhancements that Microsoft made to block selecting in VS2010. I thought that meant that they added block selecting in VS2010(as in it didn't exist in earlier versions of VS). My bad.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1