Solved

Dynamic Sort in ListBox

Posted on 1998-03-18
7
508 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
Technology Partners: 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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