Sorting List View
-
I have a list view which needs to be sorted based on a click on header.I am using the ListCtrl.SortItems() function. But it is crashing when i clicked on the header.For better understanding i have listed the code. void CDirTreeDemoView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR; this->GetListCtrl().SortItems (CompareFunc, pNMListView->iSubItem); *pResult = 0; } int CALLBACK CDirTreeDemoView::CompareFunc (LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort) { ITEMINFO* pItem1 = (ITEMINFO*) lParam1; ITEMINFO* pItem2 = (ITEMINFO*) lParam2; int nResult = 0; switch (lParamSort) { case 0: // File name nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName); break; case 1: // File size nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow; break; case 2: // Date and time nResult = ::CompareFileTime (&pItem1->ftLastWriteTime, &pItem2->ftLastWriteTime); break; } return nResult; } When i debuged lParam1 and lParam2 are getting value as '0'. Any hekp please Thanks
-
I have a list view which needs to be sorted based on a click on header.I am using the ListCtrl.SortItems() function. But it is crashing when i clicked on the header.For better understanding i have listed the code. void CDirTreeDemoView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR; this->GetListCtrl().SortItems (CompareFunc, pNMListView->iSubItem); *pResult = 0; } int CALLBACK CDirTreeDemoView::CompareFunc (LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort) { ITEMINFO* pItem1 = (ITEMINFO*) lParam1; ITEMINFO* pItem2 = (ITEMINFO*) lParam2; int nResult = 0; switch (lParamSort) { case 0: // File name nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName); break; case 1: // File size nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow; break; case 2: // Date and time nResult = ::CompareFileTime (&pItem1->ftLastWriteTime, &pItem2->ftLastWriteTime); break; } return nResult; } When i debuged lParam1 and lParam2 are getting value as '0'. Any hekp please Thanks
Silly question, but... Are you setting lParam when inserting the items in the list? -- jlr http://jlamas.blogspot.com/[^]
-
I have a list view which needs to be sorted based on a click on header.I am using the ListCtrl.SortItems() function. But it is crashing when i clicked on the header.For better understanding i have listed the code. void CDirTreeDemoView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR; this->GetListCtrl().SortItems (CompareFunc, pNMListView->iSubItem); *pResult = 0; } int CALLBACK CDirTreeDemoView::CompareFunc (LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort) { ITEMINFO* pItem1 = (ITEMINFO*) lParam1; ITEMINFO* pItem2 = (ITEMINFO*) lParam2; int nResult = 0; switch (lParamSort) { case 0: // File name nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName); break; case 1: // File size nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow; break; case 2: // Date and time nResult = ::CompareFileTime (&pItem1->ftLastWriteTime, &pItem2->ftLastWriteTime); break; } return nResult; } When i debuged lParam1 and lParam2 are getting value as '0'. Any hekp please Thanks
This link should help http://www.codeproject.com/buglist/listcontrolsortitemsbug.asp
-
I have a list view which needs to be sorted based on a click on header.I am using the ListCtrl.SortItems() function. But it is crashing when i clicked on the header.For better understanding i have listed the code. void CDirTreeDemoView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*) pNMHDR; this->GetListCtrl().SortItems (CompareFunc, pNMListView->iSubItem); *pResult = 0; } int CALLBACK CDirTreeDemoView::CompareFunc (LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort) { ITEMINFO* pItem1 = (ITEMINFO*) lParam1; ITEMINFO* pItem2 = (ITEMINFO*) lParam2; int nResult = 0; switch (lParamSort) { case 0: // File name nResult = pItem1->strFileName.CompareNoCase (pItem2->strFileName); break; case 1: // File size nResult = pItem1->nFileSizeLow - pItem2->nFileSizeLow; break; case 2: // Date and time nResult = ::CompareFileTime (&pItem1->ftLastWriteTime, &pItem2->ftLastWriteTime); break; } return nResult; } When i debuged lParam1 and lParam2 are getting value as '0'. Any hekp please Thanks
When populating the list, are you also calling
SetItemData()
?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb