Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why isn't CListCtrl::SortItems working ?

284 views
Skip to first unread message

Robert Quirk

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to
I typed in some code which was based on a MSDN example of
CListCtrl::SortItems here is a snippet:

static int CALLBACK ListCtrlCompare(LPARAM lParam1, LPARAM lParam2, LPARAM
lParamSort)
{
CListCtrl *pListCtrl = (CListCtrl *)lParamSort;

CString str1 = pListCtrl->GetItemText(lParam1,0);
CString str2 = pListCtrl->GetItemText(lParam2,0);

return 1;
}

...
m_pListCtrl = new CListCtrl;
VERIFY(m_pListCtrl->SubclassDlgItem(IDC_LIST,this));
m_pListCtrl->InsertItem(0,"1");
m_pListCtrl->SetItem(0,1,LVIF_TEXT,"D",0,0,0,0);
m_pListCtrl->InsertItem(1,"2");
m_pListCtrl->SetItem(1,1,LVIF_TEXT,"C",0,0,0,0);
m_pListCtrl->InsertItem(2,"3");
m_pListCtrl->SetItem(2,1,LVIF_TEXT,"B",0,0,0,0);
m_pListCtrl->InsertItem(3,"4");
m_pListCtrl->SetItem(3,1,LVIF_TEXT,"A",0,0,0,0);
m_pListCtrl->SortItems(ListCtrlCompare,(LPARAM)m_pListCtrl);
...


I add four items to the list control. When I call SortItems after adding
them the function ListCtrlCompare is called four times ( once for each item
? ). The problem is that when I step through with the debugger within the
function ListCtrlCompare, the parameters lParam1 and lParam2 ( which are
meant to be the indexes of the items in the list control being compared )
never change from 0. What is going on ?

Do these things only ever happen to me ? Has Microsoft neglected to tell me
that SortItems only works with the list style LVS_SECRETSQUIRREL ?

Ta.

Eric Vella

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to

Robert Quirk wrote:

Unfortunately, despite the confusing documentation, lParam1 and lParam2 are
item data, not item indexes. Use SetItemData() to set these parameters.

David Lowndes

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to
>I typed in some code which was based on a MSDN example of
>CListCtrl::SortItems

Bad example!

>.. the parameters lParam1 and lParam2 ( which are


>meant to be the indexes of the items in the list control being compared )
>never change from 0. What is going on ?

The example fails to tell you that you need to store some sort order
index in the list control's item data - the item data value is what's
passed to the sort callback, not the index of the item itself.

Dave
--
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.

Robert Quirk

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to
David, thank you very much. It is difficult to program when both the MFC
documentation and MSDN documentation both mislead you and the furthest you
can debug is the SendMessage call.

David Lowndes <dav...@mvps.org> wrote in message
news:8lg25s03npoocka41...@4ax.com...

Robert Quirk

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to
Thank you very much -it's difficult to program when both the MFC
documentation and the MSDN documentation omits facts- no, sorry misleads
you - and the deepest you can debug to is to the SendMessage call.
0 new messages