Change the Sorting behavior of CListBox on the fly

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
tullheadAsked:
Who is Participating?
 
Answers2000Connect With a Mentor Commented:
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
 
PriyeshCommented:
cbobox.ModifyStyle(0, LBS_SORT) ;
0
 
tullheadAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
PriyeshCommented:
try cboBox.ModifyStyle(0, CBS_SORT|CBS_HASSTRINGS) ;
0
 
PriyeshCommented:
sorry ,
lBox.ModifyStyle(0, LBS_SORT|LBS_HASSTRINGS) ;
0
 
KurtVonCommented:
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
 
tullheadAuthor Commented:
KurtVon --

Could you show me how to do this override?
0
 
KurtVonCommented:
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
 
KurtVonCommented:
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
 
KurtVonCommented:
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
 
tullheadAuthor Commented:
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
 
KurtVonCommented:
Nope.  I forgot the list box had to be owner draw.  Adding that probably makes it more trouble than the two-listbox solution.
0
 
tullheadAuthor Commented:
MGC sure is inconvenient sometimes!  You'd think you could
just twiddle a property field to enable/disble sorting...
0
 
KurtVonCommented:
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
 
tullheadAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.