How do I sort the items in a list box?

R

Robin Tucker

How do I sort the items in a list box?

I am using a class derived from IComparer to sort items on columns in a
ListView, but the ListBox doesn't support this kind of facility. The
"items" in my list box aren't just strings, they are a class I have defined
for each list item. There is a comparitor function, but how can I write -
ListBox.Items.Sort() in this instance?

Do I need to do my own quicksort?
 
R

Robin Tucker

Hi,

Thanks for this - although I have written a "generic" comparer (well it
compares 2 instances of the objects I want it to compare, on the keys I want
it to compare on). I'm using this in the ListView to sort, but the ListBox
is different. With the list view, all I need to do is set the comparer when
the user clicks on a column and it sorts automatically. Listbox's are more
primitive. I just have a ListBox.ObjectCollection, which doesn't support
sorting, so I have to do it myself somehow. Just thought there might be a
quick and easy way like with the ListView.

Cheers
 
C

Cor

Hi Robin,

With a little bit changed the code that EricJ did supply us today I got with

Dim li As clsListItem = New clsListItem("Robbedoes", "Marsupulanie")
ListBox1.Items.Add(li)
li = New clsListItem("Suske", "wiske")
ListBox1.Items.Add(li)
li = New clsListItem("Lucky Luke", "Joly Jumper")
ListBox1.Items.Add(li)
ListBox1.Sorted = True

On screen as:
Lucky Luke,
Robbedoes
Suske

I hope this is something for you,

Cor
 

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