ckchessmaster
ckchessmaster

Reputation: 67

Having trouble with text alignment in C#

So i'm trying to format the output of a listbox. Basically I want the name of the item left aligned and the cost of the item right aligned. Here is my ToString() method that i'm using:

 public override string ToString() {
     return String.Format("{0, -20}{1, 10:C2}", name, cost);
 }

Then here is a picture of what the resulting output is doing:

listbox output

Am I missing something simple here? From everything I've read this should format my output properly. I've read some stuff about using ListView instead however for the assignment I'm required to use a ListBox. Any help would be appreciated!

Upvotes: 0

Views: 575

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

You just need to set the font of the control to something with a fixed-width.

listBox1.Font = new Font(FontFamily.GenericMonospace, 10);

listBox1.Font = new Font("courier", 10);

Upvotes: 1

Related Questions