tullhead
asked on
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
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
cbobox.ModifyStyle(0, LBS_SORT) ;
ASKER
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.
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.
try cboBox.ModifyStyle(0, CBS_SORT|CBS_HASSTRINGS) ;
sorry ,
lBox.ModifyStyle(0, LBS_SORT|LBS_HASSTRINGS) ;
lBox.ModifyStyle(0, LBS_SORT|LBS_HASSTRINGS) ;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
KurtVon --
Could you show me how to do this override?
Could you show me how to do this override?
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(i nt nIDCtl, LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{
}
Then, on OnCompareItem just check the control ID and the sort status:
int CMyDialog::OnCompareItem(i
{
}
Sorry, something wierd happened there. In the function you do something like
if (nCtlID = m_nMyItem)
{
if (m_bSorting)
{
CString str1, str2;
MyList.GetText(lpCompareIt emStruct-> itemID1, str1);
MyList.GetText(lpCompareIt emStruct-> itemID2, str2);
return str1.Compare(str2);
}
else
return 0;
}
if (nCtlID = m_nMyItem)
{
if (m_bSorting)
{
CString str1, str2;
MyList.GetText(lpCompareIt
MyList.GetText(lpCompareIt
return str1.Compare(str2);
}
else
return 0;
}
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.
ASKER
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?
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?
Nope. I forgot the list box had to be owner draw. Adding that probably makes it more trouble than the two-listbox solution.
ASKER
MGC sure is inconvenient sometimes! You'd think you could
just twiddle a property field to enable/disble sorting...
just twiddle a property field to enable/disble sorting...
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.
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.
ASKER
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!)
all along, I just figured there must be a better way,
but maybe there's not!)