?
Solved

Change the Sorting behavior of CListBox on the fly

Posted on 2003-03-03
15
Medium Priority
?
1,553 Views
Last Modified: 2013-11-20
I generally set up that I want my CListBox to sort or
not to sort when I create it in MSDEV.

Now I need to change wether it sorts or not "on the fly".

How to I do that?  Something like ModifyStyle with LBS_SORT, but I don't quite see how to do it.

Thanks
0
Comment
Question by:tullhead
[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
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 8061909
cbobox.ModifyStyle(0, LBS_SORT) ;
0
 

Author Comment

by:tullhead
ID: 8062043
Yes, that what I tried as my guess, but it doesn't work.
Note that I need a way to both
a) make it sort
b) make it not sort
But in any case your suggestion does not change the action
of the list.
0
 
LVL 9

Expert Comment

by:Priyesh
ID: 8062177
try cboBox.ModifyStyle(0, CBS_SORT|CBS_HASSTRINGS) ;
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 9

Expert Comment

by:Priyesh
ID: 8062186
sorry ,
lBox.ModifyStyle(0, LBS_SORT|LBS_HASSTRINGS) ;
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 8064239
This won't work because the listbox doesn't detect this change and resort. You can't do anything about that unless you have source to Windows, i.e. you are Microsoft

Work round:
1. Create 2 listboxes as same size and position
One sorted, one not
2. Show/Enable one using ShowWindow and EnableWindow
3. Hide/Disable the other using ShowWindow and EnableWindow
4. When you fill the list - fill both lists
5. When you want to switch between modes, use 3 & 4 to switch which one is shown.  If you are clever, you could get the cursor selection from the currently shown one and set the equivalent selection (plus scroll into view) the equivalent item in the one about to be shown.
6. When you close the dialog, to find out which item is selected in the visible list (use IsWindowVisible to test each)

Alternative:
Use CListCtrl with a single column. Resort when they click the button. Your compare callback can use some flag (which is modified by the button) to determine whether to sort or not.
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8064775
Or, make a single list box that always sorts and override OnCompareItem.  Just always return 0 if the list is unsorted, and genuinely compare the items if it is sorted.
0
 

Author Comment

by:tullhead
ID: 8065368
KurtVon --

Could you show me how to do this override?
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8065614
In the owning dialog just use the MFC wizard to add WM_COMPAREITEM message handling, or create a child dialog and add the WM_COMPAREITEM reflection.  It's basically one of the three callbacks for listboxes (WM_DRAWITEM and WM_MEASUREITEM being the other two).

Then, on OnCompareItem just check the control ID and the sort status:

int CMyDialog::OnCompareItem(int nIDCtl, LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{

}
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8065641
Sorry, something wierd happened there.  In the function you do something like

if (nCtlID = m_nMyItem)
{
  if (m_bSorting)
  {
    CString str1, str2;
    MyList.GetText(lpCompareItemStruct->itemID1, str1);
    MyList.GetText(lpCompareItemStruct->itemID2, str2);
    return str1.Compare(str2);
  }
  else
    return 0;
}
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8065664
Oh, and I should add this only works as the items are inserted, so you still need to re-insert the items into the list box.
0
 

Author Comment

by:tullhead
ID: 8069607
Doesn't seem to work.  To make things real simple, I
used MFC wizard to add WM_COMPAREITEM message handling
and then I made that function always return 0.  The
list controls were created with "sort" turned on, and
even doing the above did not make them stop sorting.

Got another idea?
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8071980
Nope.  I forgot the list box had to be owner draw.  Adding that probably makes it more trouble than the two-listbox solution.
0
 

Author Comment

by:tullhead
ID: 8072889
MGC sure is inconvenient sometimes!  You'd think you could
just twiddle a property field to enable/disble sorting...
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 8072953
It's an optimization issue.  Back when windows first started out, sorting had to be done on insert since it was just a binary search to find the insert point.  Nice and fast.

Now computers are fast enough, but maybe some legacy programs still rely on that behaviour, so only insert actually sorts.

Can't really think of a real world example where you would want to take advantage of this, though.

Personally I unless you have a good reason not to, I think Answers2000's idea about the CListCtrl is your best bet.
0
 

Author Comment

by:tullhead
ID: 8073232
OK, I guess this is the way (its what I've been doing
all along, I just figured there must be a better way,
but maybe there's not!)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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