Complex list box

G

Gary Hillerson

I'm trying to figure out the best way to create a multi-column list
box in VBA that's like the ones Explorer and lots of other programs
use. The screen presents a list of items with different columns, and
the user user can click on a column head to sort by that column.

I'm not sure, but I'm guessing this is one large, scrolling list box
with tabs in the strings to "columnize" and each column heading set up
as a button that triggers the sorting. I can do that and can apply a
sorting algorithm, but I'm wondering if there are easier or canned
solutions available.

I need any solution to be compatible with Word 97 and above.

Any info appreciated.


thanks in advance,
gary
 
J

Jonathan West

Hi Gary,

You can have a multi-column list box in VBA. If you look it up in Help it
will tell you how.

There is a ColumnHeads property for the ListBox control on VBA UserForms,
that hives you those grey column headings, but it is bugged and useless, so
don't bother even trying to do anything with it.

The simplest way of filling a multi-column listbox is to have a Variant
containing a 2-dimensional array, and assign it to the List property of the
listbox, something like this


Dim vList as Variant
ReDim vList(2, 2)
vList(0, 0) = "Apples"
vList(0, 1) = "Cox"
vList(0, 2) = "Class 1"
vList(1, 0) = "Apples"
vList(1, 1) = "Granny Smith"
vList(1, 2) = "Class 1"
vList(2, 0) = "Pears"
vList(2, 1) = "Conference"
vList(2, 2) = "Class 2"
ListBox1.List = vList

To sort the list on any particular column, you can adapt any of a whole
bunch of sorting algorithms that are available on the Internet. Just type VB
and Quicksort into Google and you'll soon find plenty of examples which you
can use and adapt.

After that, all that is left is to work out how to trigger a sort. For that,
i suggest you place small CommandButtons where the column heads should go.
It won't look exactly like it does in Explorer, but it will be fairly close.
The Click event of each commant button should load the List property into a
variant array, sort the array, and then reassign the sorted array back top
the List property.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
G

Gary Hillerson

As always, thanks very much, Jonathan......especially for steering me
away from the ColumnHeads property. You've probably saved me a lot of
time and headaches.

thanks,
gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top