Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting Items in a Combo Box...

175 views
Skip to first unread message

BRM

unread,
Aug 18, 2004, 4:57:02 PM8/18/04
to
I have a Combo Box that is sub-classed. I want the items in the list to be
sorted by the lParam of the item and then alphabetically (upper and lower
case separately).

The dialog control being sub-classed has the Sort property set to true, but
it isn't sorting them, or if it is, it is doing so in reverse of what I want.
For example, the list contains "r, D, .s, .., and ." which is the order is
displays them in from top to bottom, but I want the reverse ("., .., .s, D,
r").

I tried setting up the CompareItem(), but it doesn't seem to be called.

FWIW:
- The ComboBox is a Drop Down (CBS_DROPDOWN) style.
- The data is being entered internally by the class via InsertString(), and
then the lParam data is set via SetItemData().

Am I doing something wrong?

TIA,

BRM

Mike Stephenson

unread,
Aug 19, 2004, 12:48:19 AM8/19/04
to
CompareItem() is only called if your combo box is owner-drawn.

If you don't want to go this route, then how about having a sorted data store
that backs the combo box?

There is a very nice sorted array class on CodeProject, CSortedArray, that I
have used and which works fabulously, and it has a nice callback Compare()
function that you can code to sort any way you like. It has a member function
called OrderedInsert, which returns the index at which the item was inserted,
which is just what you need.

In fact, here is the article that contains CSortedArray from CodeProject. The
author is doing something similar to what you want to do, although he is usting
a tree control:

http://www.codeproject.com/treectrl/filetree.asp

Backing up a bit, are strings added to the box dynamically, i.e., by user
interaction? If not, simply sort the strings the way you want and then add them
to the combo box in your sorted order.

If strings are added on the fly by the user, and you want them sorted, you could
add the string to a sorted array, which backs the combo box items, get the index
of the newly added string in the array, and use InsertItem to insert it in the
combo box at this index.

Whenever you add an item to the box, add it to the array first, get the index,
then add it to the box with this index. Whenever you delete an item from the
box, also delete it from the array, i.e., you must keep the array and the box in
synch.

I can't think of any way to control the sorting using just the combo box, unless
you want the default sorting, which you don't, or unless you set the box to
owner draw.

0 new messages