Solved

Dynamic Sort in ListBox

Posted on 1998-03-18
7
510 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
Industry Leaders: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

688 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