# VBForums CodeBank > CodeBank - C# >  Formatting Fixed-width Text Output

## jmcilhinney

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:
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, 6, 19), 2);builder.AppendLine();builder.AppendFormat(format, 2, "Paul", new DateTime(1974, 4, 23), 4);builder.AppendLine();builder.AppendFormat(format, 3, "Mary", new DateTime(1980, 12, 4), 1);builder.AppendLine(); 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.  :Wink: 

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.

----------


## firoz.raj

why it says .Year, Month, and Day parameters describe an un-representable DateTime.let me know please .following are the 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();

        }
    }
}
```

----------


## jmcilhinney

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".

----------


## kfcSmitty

> 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.




> ```
> 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);
> ```

----------


## jmcilhinney

> Just an FYI, he copied your code. In your example, you have it as year, day, month and not year, month, day.


 :LOL:  Well, I guess I need to take some of the blame then.  :Blush:   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.

----------

