listbox with columns

hey all,

need to add 2 recordset items to the same listbox.

the character length varies among the strings, but i am trying to get them to line up.

for example:

Home 800.555.1234
Office 800.555.2697


Home          800.555.1234
Office           800.555.2697
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Add this code to a new form that has a list box (List1):

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    'This may need to be adjusted

'Clear any existing tabs
SendMessageLong List1.hWnd, LB_SETTABSTOPS, 0&, 0&
SendMessageArray List1.hWnd, LB_SETTABSTOPS, CLng(UBound(TabStops) - LBound(TabStops) + 1), TabStops(LBound(TabStops))

List1.AddItem "Home" & vbTab & "800.555.1234"
List1.AddItem "Office" & vbTab & "800.555.2697"

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
Since you only are calling this once, you can abreviate the following line:
SendMessageArray List1.hWnd, LB_SETTABSTOPS, CLng(UBound(TabStops) - LBound(TabStops) + 1), TabStops(LBound(TabStops))

As follows:
SendMessageArray List1.hWnd, LB_SETTABSTOPS, 1, TabStops(0)

Richie_SimonettiIT OperationsCommented:
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

The standard listbox does not support multiple columns, but if you set the font to Courier New (or other fixed-point font like Win2000's Lucida Console) it will be pretty easy to line things up.

Alternately, you can get a listbox that supports multiple columns, like the one that comes with Access and is part of the MS Forms 2.0 Object Library. (Note that this particular one can only be distributed with permission from MS, although you could try to write your own!)
Anthony PerkinsCommented:

>> it will be pretty easy to line things up <<
This is basically what the code I posted does, without the need to use Fixed Fonts.

If you already have reference to "Microsoft Windows Common Controls" component, use ListView control in Report view.
Good job, Anthony.

However, I always try to avoid API calls when not necessary, simply because they are likely to break in future versions, especially with .net looming...

In this case, they might be necessary...if you want a proportional font.
Have to agree with ameba on this one use a ListView control instead of a List control. No messing around with API calls, Fonts, positioning columns etc.

jsopherAuthor Commented:
thanks man,

worked like a charm - exactly what i wanted.
Richie_SimonettiIT OperationsCommented:
Jst, optionally, you could add microsoft forms component to toolbox and use listbox from it since it has columns property.
It uses resources and i don't know if it is redistributable.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.