Right-justify ListBox items? Is this possible?

donaldc104

Freshman
Joined
Jan 4, 2003
Messages
32
Is it possible to right-justify the items in a ListBox?

The Label has a property called TextAlign. But I can't find any alignment for the ListBox. I only need Left-or-Right alignment.

I tried to use a space-padded Format like this:
Visual Basic:
 lstList1.Items.Add(Format(dNumber, "0,-10:C "))
but could not get it to work (space-padding refused to appear).

Does anybody know a trick that I could use?

- donaldc104
 
I tried the RightToLeft property (set to True) and that worked.

I can't say I've ever seen a ListBox that was right-aligned though. What kinds of data are you putting in the listbox? Could you use some other control, maybe?

-Nerseus
 
RightToLeft right-justifies ListBox items.

It works ! The RightToLeft property right-justifies OK. I thought this property was for Hebrew or Arabic languages.

I'm displaying columns of numbers, and it looks better if the larger values stick out. See attached image.

Unfortunately, setting {RightToLeft = True} also moves the scroll bars to the left side of the box. But I can work around that.

I first tried using a DataGrid. I like its display better, but it is so complex (you must drag along an entire data base, etc). Is there some other control you would recommend ?

Thanks for the tip ! :D
 
Normally a DataGrid would be more "standard" but nothing says you can't use whatever control you like. I do see what you mean with the DataSet and DataGrid being a bit more cumbersome than a simple listbox. You could use an ArrayList for the DataGrid, but you'd have to define a custom object to put in there so that the Grid displayed what you want.

And you're right about the RightToLeft property. It's purpose is for different languages, not for string formatting. But a ListBox wasn't meant to hold right-aligned text (that I've ever seen) so it worked well :)

I'm not sure how many numbers you're displaying, but I can think of two other alternatives. A multiline textbox (readonly) might work. You could also try a ListView. It's a tad bit more work than a listbox but might be worth it.

Good Luck!

-Nerseus
 
ListView will work OK

Thanks for the tip on the ListView !
I had previously eliminated it as a possibility, based on the documentation that I'd read. But when I read your suggestion, I researched it deeper. It appears that it will do the trick for my program.
ListView is sparsely (poorly) documented but it's an amazingly flexible control. DataGrid is better documented but it's not a match for me.
 
Back
Top