Friends , Is there any way for data alignment in the list box. My code is given below

Private Sub TxtFdt_KeyPress(KeyAscii As Integer)
Dim L, L1, L2, L3, L4, L5 As Integer
Select Case KeyAscii
Case vbKeyReturn
If TRS.State = adStateOpen Then TRS.Close
TRS.Open "SELECT * FROM Tran WHERE Ac = '" & TxtAc.Text & "'" & "and Acno=" & Val(TxtAcno.Text) & "and Tdate>= #" & TxtFdt.Text & "#" & "ORDER by Tdate,Trnno", CN, adOpenStatic, adLockOptimistic
If TRS.RecordCount = 0 Then
MsgBox "NO TRANSACTION FOR THIS PERIOD !", vbInformation, "PEBBLE"
TxtFdt.SetFocus
Else
TRS.MoveFirst
List1.Clear
Do Until TRS.EOF
L = Len(Left(TRS!Tdate, 10))
L = 10 - L
L1 = Len(Left(TRS!Details, 25))
L1 = 25 - L1
L2 = Len(Left(TRS!Debit, 10))
L2 = 10 - L2
L3 = Len(Left(TRS!Credit, 10))
L3 = 10 - L3
L4 = Len(Left(TRS!Cd, 2))
L4 = 2 - L4
L5 = Len(Left(TRS!Balance, 10))
L5 = 10 - L5
List1.AddItem Left(TRS!Tdate, 12) & Space(L) & Space(3) & Left(TRS!Details, 25) & Space(L1) & Space(5) _
& Left(TRS!Debit, 10) & Space(L2) & Space(7) & Left(TRS!Credit, 10) & Space(L3) & Space(1) _
& Left(TRS!Cd, 2) & Space(L4) & Space(3) & Left(TRS!Balance, 10)
TRS.MoveNext
Loop
CmdClear.SetFocus
End If
End Select
End Sub

My aim is to display the datas from a Database table in a list box under the field heads: Date,Details,Debit,Credit,Balance.It is working fine.The datas of Debit,Credit,Balance are of Number type.So I want them to be aligned to the right of the column. In my code it is aligned to the left of the field.Is there any way to align the number datas (only) to the right of the column? .....Please help..

Dani AI

Generated

assembled each row as one long string. The VB6 ListBox has no built‑in per‑column alignment, so numbers will only line up if the visual width of each character is predictable. Two practical options follow (quick fix vs. robust solution).

A quick fix (minimal change): set the list box to a monospaced font such as Courier New and insert right‑padded formatted numbers so every numeric column occupies a fixed number of characters. Format numeric values with Format$ to control decimals and thousands separators, then pad with Right$ so the most significant digit sits at a fixed character column. Watch for negative values, currency symbols and locale decimal separators when choosing widths.

A more robust solution (recommended): replace the ListBox with a ListView (Microsoft Windows Common Controls). ListView in report mode supports true columns and per‑column alignment, so numeric columns can be right aligned and remain sortable and selectable. Example setup and population (illustrative):

With ListView1
  .View = lvwReport
  .Gridlines = True
  .ColumnHeaders.Add , , "Date",    90
  .ColumnHeaders.Add , , "Details", 240
  .ColumnHeaders.Add , , "Debit",   90, lvwColumnRight
  .ColumnHeaders.Add , , "Credit",  90, lvwColumnRight
  .ColumnHeaders.Add , , "Balance",100, lvwColumnRight
End With

Dim it As ListItem
Set it = ListView1.ListItems.Add(, , Format$(TRS!Tdate, "dd-mmm-yyyy"))
it.SubItems(1) = Left$(TRS!Details, 25)
it.SubItems(2) = Format$(TRS!Debit, "#,##0.00")

Referencing : the monospaced+padding idea is the fast route; referencing : ListView gives proper column control. For tabular numeric data, the ListView (or a grid control like MSFlexGrid/DataGrid) is the cleaner, more maintainable choice.

Recommended Answers

All 2 Replies

O.K. You need to use a monospaced font which assigns each letter, number the same space, so an i would take up the same space as a w. Than you use the Rset function, where the number indecates how much you move your items to the right- depends on the width of the listbox. Here I post a few lines of code that demonstrates the use.

Private Sub Form_Load()
    List1.AddItem ("Hello")
    Dim strItem As String * 20
    RSet strItem = "Testing"
    List1.AddItem (strItem)
    Dim strItem1 As String * 10
    RSet strItem1 = "Testing"
    List1.AddItem (strItem1)
End Sub

Instead of using the messy List Box, you may use a List View.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.