Call List3.AddItem(txtname & vbTab & txtamount & vbTab & txtqan & _vbTab & Format$(ans, "Currency"))


need help need to align the text the keep displaying like this:

toy car 1 $3.00 $3.00
card board box1 $0.50 $0.50
remote control car 1 $3.00 $3.00
cart 1 1 $1.00 $1.00

this is just the sample need it to look clean need help please the out put must be:

toy car 1 $3.00 $3.00
card board box 1 $0.50 $0.50
remote control car 1 $3.00 $3.00
cart 1 $1.00 $1.00

Dani AI

Generated

Two practical routes: use a true multi‑column control so the control does all the alignment, or keep the ListBox and format fixed‑width text yourself. pointed to the multi‑column approach as the clean solution; correctly raised deployment/redistribution concerns about Office/VBA-only controls, so prefer a control that is available on the target machines if the project will be shipped.

If switching controls is acceptable, use a ListView (report/columns mode) and let its columns hold name, qty, price and total. Column widths should be set to accommodate the longest entry so numbers align naturally. If changing the control is not possible, force alignment by using a monospaced font and padding fields to fixed widths before adding the line to the ListBox.

Sample VB6 helper (for a monospace ListBox font such as "Courier New"):

' Build a fixed-width row for a monospace ListBox font
Private Function PadRight(ByVal s As String, ByVal w As Long) As String
    If Len(s) >= w Then
        PadRight = Left$(s, w - 1) & " "
    Else
        PadRight = s & String$(w - Len(s), " ")
    End If
End Function

Public Function MakeRow(ByVal name As String, ByVal qty As String, ByVal price As Currency, ByVal total As Currency) As String
    Dim pPrice As String, pTotal As String
    pPrice = FormatCurrency(price, 2)
    pTotal = FormatCurrency(total, 2)

    MakeRow = PadRight(name, 28) & PadRight(qty, 6) _
              & Right$(String$(12, " ") & pPrice, 12) _
              & Right$(String$(12, " ") & pTotal, 12)
End Function

Troubleshooting notes: vbTab will not produce column alignment in a proportional font. Ensure the ListBox font is monospaced if using space padding, or measure string pixel widths (TextWidth on a hidden control) and compute pixel offsets for precise alignment. For long names, truncate or increase the name column width before building rows.

Recommended Answers

All 2 Replies

These two lists looks both the same. I guess this web didn't formated them well, so I don't now, what do you want to do with them exactly, but if you just want to make the lists aligned into nice columns, then try:

1) Right click on listbox and choose properties
2) Check gridlines (optionally, you'll see the cells then)
3) Set the view combo on value 3 - Report (thats necessary)
4) Set label edit on Manual (optionally, in the other case the first cell is editable by user as I remember)
5) Click on the "Column headers" tab and click four times on Insert column (necessary if you want to use my code below)
6) You can also name your columns, set their width, ...

then, for filling the list, use this:

Dim itmLIST3 As ListItem

Set itmLIST3 = List3.AddItem(txtName)
itmLIST3.SubItems(1) = txtamount
itmLIST3.SubItems(2) = txtqan 
itmLIST3.SubItems(3) = Format$(ans, "Currency")

This should work...

Celt.Max,
the code you are using is for the VBA Listbox control found in Excel, access, etc. and while it is usable from VB6, it is not redistributable...

IvanKenny,
There are two ways in which to accomplish what you want...
1.) Use a listview control.
2.) Do a bit of subclassing on the listbox. See... http://www.thescarms.com/vbasic/listbox.aspx

Good Luck

commented: get it solve my problem thx for the tip i used method 2 +0
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.