Solved

Dynamic Sort in ListBox

Posted on 1998-03-18
7
503 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

12 Experts available now in Live!

Get 1:1 Help Now