say I have a string array as:
string[] AllFruits = {"Apples $1.50",
"Oranges $1.80",
"Kiwis $3.10",
"Bananas $1.00};
I add them to the listbox as follows:
listBox1.Items.AddRange(AllFruits);
I want the name of the fruits to be aligned LEFT, and the price to be aligned RIGHT of the listbox.
How do I do that? thanks.
(x)
tanthiamhuat
476
476
I am referring to a standalone application in C#, not a webapplication. I know in ASP.NET, or using Silverlight, it can add a lots of different functionalities, like in http://www.longhorncorner.com/UploadFile/dbeniwal321/SilverlightListBox01042009231701PM/SilverlightListBox.aspx

3 answers
Here's an easy way to do this.
You'll first need to set the ListBox font to a fixed width font such as Courier New.
The following code will then left align the fruit and right align the price within a field large enough to accomodate the widest item. If you want to right align to the width of the whole listbox, then I'd simply increase 'maxLen' to the maximum number of fixed width characters it can accomodate:
answered 7 months ago by:
11603
hi Vulpes,
I understand your logic of adding extra spaces.. I try out the code, but it does not seem to work. the price are not aligned RIGHT as what I want.
Apples $1.50
Oranges $1.80
Kiwis $3.10
Bananas $1.00
answered 7 months ago by:
476
11603
All I can say is that it worked fine when I tried it myself. Have you remembered to use a fixed width font for the ListBox - if you don't then it will come out a mess because different letters/digits will have different widths.
1822
Can I just note that the ListBox is not the right control for this behavior. A better choice would be ListView or DataGridView I think.
string[] AllFruits = {
};
How to fetch database (Column values) into this array?
answered 7 months ago by:
396
476
hi Vulpes, could you elaborate where is the settings for fixed width font for the ListBox?
11603
In the Properties Box for your ListBox control, select the Font property and choose a fixed width font. I used Courier New 8 pt (actually 8.25 pt) when I tried it myself.
476
that is wonderful, it works. Thank You very much.