Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right align list box data

Status
Not open for further replies.

kliz0328

Programmer
Nov 7, 2005
138
US
Is it possible to right align list box data?
 
I am trying to set the right to left property to true, but everytime I click on true, it goes right back to false?
 
Right to Left is for languages such as Arabic that are read from right to left. If you don't have that installed on your system then you can't set "Right to Left" to TRUE.

I suppose that you could pad items on the left as you load them e.g

List1.AddItem Right(Space(40) & "Text For List", 40)

You would probably need to use a non-proportional font like Courier New to make that work.

 
kliz0328,
I know, it looks crazy, but works.

Golom,
Your example is on the right track. Needs to be twicked a bit. I did not try to optimize code.

Private Sub Command1_Click()
ReDim intLen(List1.ListCount) As Integer
Dim intList As Integer
Dim intMaxLen As Integer

List1.Clear
List2.Clear
List2.Visible = False
'List2.Sorted = True - set in design

List1.AddItem "ABCDEFG"
List1.AddItem "ABCDEF"
List1.AddItem "ABCDE"
List1.AddItem "ABCD"
List1.AddItem "ABC"


For intList = List1.ListCount - 1 To 0 Step -1
List2.AddItem Len(List1.List(intList))
Next intList

intMaxLen = List2.List(List2.ListCount - 1)

For intList = List1.ListCount - 1 To 0 Step -1
List1.List(intList) = String(20 - Len(List1.List(intList)) + intMaxLen - Len(List1.List(intList)), " ") & List1.List(intList)
Next intList

End Sub

vladk
 
Is it possible? Yes. Here is an example (not a full solution), although I haven't time to explain it all right now (I'll leave that as an exercise for the reader...). You'll need a form with a listbox. The copy and paste this code
Code:
[blue]Option Explicit
Private Const LB_SETTABSTOPS = &H192
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Const sChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

Private Function GetCalcRightMargin(myList As ListBox) As Long
    Dim avg As Long
    Dim oldfont As Font
    
    Set oldfont = myList.Parent.Font
    Set myList.Parent.Font = myList.Font
    avg = myList.Parent.TextWidth(sChars) / Len(sChars)
    Set myList.Parent.Font = oldfont
    GetCalcRightMargin = -MulDiv(myList.Width - 4, 4, avg)
End Function

Private Sub Form_Load()
    Dim TabArray(0) As Long
    
    List1.AddItem vbTab & "Hello"
    List1.AddItem vbTab & "This is an example"
    List1.AddItem vbTab & "not complete"
    
    TabArray(0) = GetCalcRightMargin(List1)
    Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
    Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 1&, TabArray(0))
    
    List1.Refresh
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top