Link to home
Start Free TrialLog in
Avatar of BBaron
BBaron

asked on

how to Right Justify a Listbox

Hello,

Is it possible to right justifiy a list box?
eg if you have numeric data to 2 decimal places it comes out left justified which looks real BAD


any ideas?

cheers
Ben
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to pad it with spaces:

Instead of List1.AddItem CStr(MyNumber)

Use

List1.AddItem Right(String(10," ") & Format(MyNumber,"##9.99"),10)


I.e., add ten spaces to the front of the formatted number then get the right 10 characters from it. Of course this is not going to work properly still if you use proportional fonts in the listbox. Change the font style to courier or some other non-proportional font and it will line up properly.
Avatar of Ryan Chong
Hi TimCottee,

> Change the font style to courier or some other non-proportional font and it will line up properly.

That's Should solve the problem : )

But how to align exact to the right?
If you want Exact to the right hand edge then there are three things to work out,

1) width of listbox
2) width of scrollbar (2a - is the scrollbar displayed)
3) width of single character in non-proportional font chosen.

Take this information to determine how many characters across can be displayed, pad the formatted text with this number of characters and use as above. So in essence get this number and use it in place of the 10 I used above.

Now this of course is still not 100% precise, to make it more so you would have to create a tabstop on the left to shift the subsequent text enough to the right to line it up with the right-hand edge. Possible but probably overkill. You could also subclass the listbox and do it, this would also work for proportional fonts but of course then you have a lot of work to do. The question really is whether the effort required is more than the benefit from such effort.
Avatar of harsh008_k
harsh008_k

hi Baron
I think the best way is to make it a string before using additem.Itz as simple as that.All the items comes will have the same alignment

got it, thanks.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this,

This example uses fixed length string.

Add a Listbox and paste this code.

----------------------------------

Const FIELD_LENGTH As Integer = 13

Private Sub Form_Load()

    Dim strItems As String * FIELD_LENGTH
   
    RSet strItems = "A"
    List1.AddItem strItems
   
    RSet strItems = "AA"
    List1.AddItem strItems
   
    RSet strItems = "AAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAAAAAA"
    List1.AddItem strItems
   
    RSet strItems = "AAAAAAAAAAAAA"
    List1.AddItem strItems
   
    List1.Width = 1700

End Sub
Avatar of BBaron

ASKER

Bang on target that one,
Cheers for all the other suggestions



ben