Solved

how to Right Justify  a Listbox

Posted on 2001-08-16
8
293 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:BBaron
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6392383
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.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6392530
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?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6392580
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.
0
 
LVL 1

Expert Comment

by:harsh008_k
ID: 6392603
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6392606
got it, thanks.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 6393227
Try this:
Add a ListBox (List1) and a Command Button (Command1):

and add this code:

Private Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Sub Form_Load()

ReDim TabStops(0) As Long
TabStops(0) = -50
SetTabStops List1.hWnd, TabStops()

End Sub

Private Sub Command1_Click()

List1.AddItem vbTab & "1.23"
List1.AddItem vbTab & "12.34"
List1.AddItem vbTab & "123.45"
List1.AddItem vbTab & "1234.56"
List1.AddItem vbTab & "12345.67"
List1.AddItem vbTab & "123456.78"
List1.AddItem vbTab & "1234567.89"
List1.AddItem vbTab & "12345678.90"

End Sub


Private Sub SetTabStops(hWnd As Long, TabStops() As Long)
Const LB_SETTABSTOPS = &H192

'Clear any existing tabs
SendMessageLong hWnd, LB_SETTABSTOPS, 0&, 0&
SendMessageArray hWnd, LB_SETTABSTOPS, CLng(UBound(TabStops) - LBound(TabStops) + 1), TabStops(LBound(TabStops))

End Sub

0
 
LVL 2

Expert Comment

by:RSivakumar_Sofil
ID: 6395804
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
0
 

Author Comment

by:BBaron
ID: 6396133
Bang on target that one,
Cheers for all the other suggestions



ben
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now