Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding strings to a listBox

Status
Not open for further replies.

vijraj

Programmer
Mar 14, 2007
11
US
Hi all!
I am trying to add a few strings to a listbox and using String.format to format the string being added.

Sample:
listBox1.items.add("{0,-15}{1,-15}{0,-15}{0,-15}{0,-15}{0,-15}{0,-15}",str1,str2.str3,str4,str5,str6,str7);

When I use this in a loop to add items,they don't allign with the rest of the items.

here is a sample of the output.
(form1 and inside listBox1)
AnimalName AnimalType FoodIntake FoodValue
Vijju Monkey 200 205
vij Bird 22 20


Any advice is appreciated.
Thanks in advance.. [2thumbsup].
 
Hi,

yes, ofcource they are not aligned. That's because of the font - each char has different width. To fix it, do one of the below:

1. Use a font with all char same width (e.g. courier new) and add padding at the right. (see the ".padleft" and ".padright" methods of a string ... and use a space as the padding char).

2. The listbox is not the correct control. Use a grid (whatever) or a listview if you are using .NET 2.0


Hope these help
 
Thanks for ur tip. I am new to this .net world.
Can you please give me a example of how to check for font sytles of listbox? Thankyou.
 
I would not encourage you on doing that way ..with a listbox.
Instead (and believe me it is better) read some lines of code ( where it shows you how to add a datatable to a datagrid.

The concept is simple:
1. create a datatable and give it a name.
2. Create the columns and add them to it. (Yours are AnimalName, AnimalType, FoodIntake and FoodValue)
3. Load the items (using a datarow) to the datatable. Your rows are the 2 that you shows us, and the values (column type) are: Vijju, Monkey, 200 and 205.
4. Last, bind the datatable to the datagrid control.

It is in VB but it is easy to convert.
 
Thanks .I adjusted the font in properties and I got it.[2thumbsup][bigglasses]
 
Is the Datagrid good for a variable number of rows?
Just to learn things.
 

Font size?

Do yourself a favor and use a listview, not a listbox. Then set the view to Details and add the columns you're looking for.

Then add you item to the listview and add subitems after. You can align each column that way.
 
Hi,

listview exists in 2003 version! So as JurkMonkey and I have said, with this instead of the listbox.
* The first item is the "text" and the others are the "subitems". *

The 'original' use of a datagrid is to show data from a database. Therefore, it can handle great ammount of rows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top