Solved

how to Right Justify  a Listbox

Posted on 2001-08-16
8
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 52

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month6 days, 21 hours left to enroll

623 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