Results 1 to 5 of 5

Thread: Formatting Fixed-width Text Output

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Formatting Fixed-width Text Output

    VB version here.

    Lots of people ask how to display text formatted in columns. It's generally a bad idea to display a solid block of text formatted that way. More often you should use a grid control or a ListView, but if you really want to display tabular data in a TextBox, or perhaps in a file with fixed-width columns, then you'd do it much like this:
    CSharp Code:
    1. StringBuilder builder = new StringBuilder();
    2. string format = "{0,-4}{1,-10}{2,-12:d}{3,8}";
    3.  
    4. builder.AppendFormat(format, "ID", "Name", "DOB", "Children");
    5. builder.AppendLine();
    6. builder.AppendFormat(format, 1, "Peter", new DateTime(1969, 6, 19), 2);
    7. builder.AppendLine();
    8. builder.AppendFormat(format, 2, "Paul", new DateTime(1974, 4, 23), 4);
    9. builder.AppendLine();
    10. builder.AppendFormat(format, 3, "Mary", new DateTime(1980, 12, 4), 1);
    11. builder.AppendLine();
    12.  
    13. this.textBox1.Text = builder.ToString();
    The secret there is the format string. You can use a format string with a StringBuilder.AppendFormat, Console.WriteLine, String.Format, StreamWriter.WriteLine and other places, so your output options are many. Let's take a closer look at that format string.

    It includes 4 format specifiers, so it will accept 4 values to format. You'll see that each call to AppendFormat passes the format string followed by 4 values. Funny, that.

    The first format specifier ({0,-4}) takes the first value ({0,-4}) and pads it out with whitespace to 4 characters ({0,-4}), left-aligning the string ({0,-4}).

    The second format specifier take the second value, pads it out to 10 characters and left-aligns it.

    The third format specifier ({2,-12:d}) takes the third value, pads it out to 12 characters, left-aligns it and displays it in the short date format ({2,-12:d}).

    The fourth format specifier takes fourth value and pads it out to 8 characters, but this time the field width is greater than zero so it is right-aligned.

    Note that to actually view the output in columns you must use a fixed-width font. Notepad will do that by default but if, if you're displaying the data in a TextBox, you must set the Font to Courier New or some other appropriate value.
    Last edited by jmcilhinney; Oct 22nd, 2013 at 05:46 PM. Reason: Fixed DateTime constructor parameter order.

  2. #2
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Formatting Fixed-width Text Output

    why it says .Year, Month, and Day parameters describe an un-representable DateTime.let me know please .following are the code
    Code:
    private void Form1_Load(object sender, EventArgs e) {
                StringBuilder builder = new StringBuilder();
                string format = "{0,-4}{1,-10}{2,-12:d}{3,8}";
    
                builder.AppendFormat(format, "ID", "Name", "DOB", "Children");
                builder.AppendLine();
                builder.AppendFormat(format, 1, "Peter", new DateTime(1969, 19, 6), 2);
                builder.AppendLine();
                builder.AppendFormat(format, 2, "Paul", new DateTime(1974, 23, 4), 4);
                builder.AppendLine();
                builder.AppendFormat(format, 3, "Mary", new DateTime(1980, 4, 12), 1);
                builder.AppendLine();
                this.textBox1.Text = builder.ToString();
    
            }
        }
    }
    Name:  FormattedIssue.jpg
Views: 956
Size:  123.7 KB
    Last edited by firoz.raj; Oct 19th, 2013 at 01:21 AM.

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Formatting Fixed-width Text Output

    Um, you might have to explain what the 19th month of the year is. You probably ought to pay closer attention to Intellisense as you type your code. You might also try reading the documentation for a type or member when it doesn't work the way you expect. Also, take note of the actual error message, which says "Year, Month and Day parameters", not "Year, Day and Month parameters".

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Formatting Fixed-width Text Output

    Quote Originally Posted by jmcilhinney View Post
    Um, you might have to explain what the 19th month of the year is. You probably ought to pay closer attention to Intellisense as you type your code. You might also try reading the documentation for a type or member when it doesn't work the way you expect. Also, take note of the actual error message, which says "Year, Month and Day parameters", not "Year, Day and Month parameters".
    Just an FYI, he copied your code. In your example, you have it as year, day, month and not year, month, day.

    Code:
    builder.AppendFormat(format, 1, "Peter", new DateTime(1969, 19, 6), 2);
    builder.AppendLine();
    builder.AppendFormat(format, 2, "Paul", new DateTime(1974, 23, 4), 4);
    builder.AppendLine();
    builder.AppendFormat(format, 3, "Mary", new DateTime(1980, 4, 12), 1);

  5. #5

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Formatting Fixed-width Text Output

    Quote Originally Posted by kfcSmitty View Post
    Just an FYI, he copied your code. In your example, you have it as year, day, month and not year, month, day.
    Well, I guess I need to take some of the blame then. That should be a lesson to anyone that, especially when something doesn't work the way you expect, ALWAYS read the documentation. Just because someone posts some code somewhere doesn't mean that it's right so use your own critical thinking and pay attention to both Intellisense (if you type the code yourself) and the actual content of error messages. Code amended.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width