Solved

how to Right Justify  a Listbox

Posted on 2001-08-16
8
299 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 51

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
LVL 51

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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