Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to Right Justify  a Listbox

Posted on 2001-08-16
8
Medium Priority
?
311 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 54

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 54

Expert Comment

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

Accepted Solution

by:
Anthony Perkins earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

886 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