Solved

Dynamic Sort in ListBox

Posted on 1998-03-18
7
507 Views
Last Modified: 2008-02-01
Hi

     I am developing an activeX control that uses a ListBox
as its child control. I have a property "Sort" that can be toggled to true/false.

 I am not creating the ListBox with the LBS_SORT property , but I am using ModifyStyle in CWnd to add/Remove LBS_SORT. Though ModifyStyle returns
TRUE ( meaning success) the ListBox still remains unsorted.

Can u help me solve this problem.

regs
-Alexei
 
0
Comment
Question by:ialexei
7 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1183621
The listbox does not actually sort itself.  It only adds items in the proper position.  As you're not adding items, no sort is performed.

Two possible solutions:

1) Delete and re-add all the items (Yech!)

2) Keep two listboxes (one sorted, the other unsorted) in the same position.  Enable and make visible one of them, disable and make invisible the other.  Add and delete items to/from both.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1183622
Oh, by the way.  This sort of questions stands a better chance of being answered in the "Windows programming" area.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183623
 A cleaner solution might be to use a the Listview control (CListCtrl)...

-=- James
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 11

Expert Comment

by:alexo
ID: 1183624
ialexei, if you explain why you reject an answer it will help providing you with one that better suits your needs.
0
 

Author Comment

by:ialexei
ID: 1183625
Hi alexo

     I am on critical project here , and the success of the control depends on implementing this feature. There has to be some
way of automatically sorting it with some API. If I follow the soln that u offered , it would cost me a lot of time , which I dont want to happen.
I expected someone else would try that & give me a better solution .  jtwine's solution seemed good. Anyway thanx for answering this question.

0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 200 total points
ID: 1183626
You could also create a derrived class from CListBox and set up a pointer arrays to the original listbox data,  in sorted order and an array containing the original order.

If you override the add and remove methods to create and maintain the sorted pointer array and override the paint method to display the data either  the original or sorted order based on the values of the arrays and the LBS_SORT flag you should have a good solution in about two hours of coding.

Additionaly, if execution time is an issue, you can use insertion sorts such as calculated address for number fields and a doubly linked list insertion sort for strings. That should give you verry good execution time besides the flexibility you require.


0
 
LVL 11

Expert Comment

by:alexo
ID: 1183627
ialexei, you cannot re-sort a listbox, no API is provided.
jtwine's solution is good.  However, keep in mind that listviews behave differently from listboxes in subtle ways.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now