?
Solved

Dynamic Sort in ListBox

Posted on 1998-03-18
7
Medium Priority
?
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 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