| | |
List View Sorting and Callback
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I have a list view report style. Im trying to sort by click on a header. For some reason it does nothing.
This Peice of code is invoked when the column is clicked.
Now Here is my Call back function
According to MSDN, the Sort Macro should handle the results of the callback just like strcmp, and switch the items accordingly. Once again, the callback function is called and works, yet it does not sort properly, what could be wrong?
This Peice of code is invoked when the column is clicked.
C Syntax (Toggle Plain Text)
if (pnm->hdr.code == LVN_COLUMNCLICK) { //DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_SORT), // NULL, (DLGPROC)SortDlgProc, 0); if(nSortDir[pnm->iSubItem]) nSortDir[pnm->iSubItem] = false; else nSortDir[pnm->iSubItem] = true; ListView_SortItems (hList, ListViewCompareProc, pnm->iSubItem); nItem1=0; return 0; }
Now Here is my Call back function
C Syntax (Toggle Plain Text)
int CALLBACK ListViewCompareProc (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { static LV_FINDINFO fi; static int nItem1, nItem2; static char szBuf1[30], szBuf2[30]; // Determine the items that we are comparing. //........................................... fi.flags = LVFI_PARAM; fi.lParam = lParam1; nItem1 = ListView_FindItem(hList, -1, &fi); fi.lParam = lParam2; nItem2 = ListView_FindItem(hList, -1, &fi); // Retrieve the item text so we can compare it. //............................................. ListView_GetItemText(hList, nItem1, lParamSort, szBuf1, sizeof(szBuf1)); ListView_GetItemText(hList, nItem2, lParamSort, szBuf2, sizeof(szBuf2)); // Return the comparison results. //............................... if (nSortDir[lParamSort] ) // ACENDING ORDER return(strcmp(szBuf1, szBuf2)); else return(strcmp(szBuf1, szBuf2) * -1); }
According to MSDN, the Sort Macro should handle the results of the callback just like strcmp, and switch the items accordingly. Once again, the callback function is called and works, yet it does not sort properly, what could be wrong?
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Since the callback function pretty much enumerates the list, I made the buffers static to save space on the stack.
I did find something funny after playing with it. The two strings that are being compared are always the same. From this I have concluded that the LPARAM value returned from the Find Item macro is pointing to the same item.
It dosn't really make sense since the two LPRAM items being compared are passed in as different values.
I'm so lost :cry:
I did find something funny after playing with it. The two strings that are being compared are always the same. From this I have concluded that the LPARAM value returned from the Find Item macro is pointing to the same item.
It dosn't really make sense since the two LPRAM items being compared are passed in as different values.
I'm so lost :cry:
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Hey I got it working!!!!!!! After digging a bit more through MSDN I came up with a solution. The problem was that finding the item index, for the purpose of getting the text is unstable within the call back procedure, the index returned by the lparam found, will always be the same. The solution was to use ListView_SortItemsEx macro instead of ListView_SortItems. The reason being is that ListView_SortItemsEx passes the intem index as the first two prarmeters of the callback procedure, where as ListView_SortItems simple passes the lparam. IT ALL MAKES SO MUCH SENSE NOW. Think about it why would you ever need to use ListView_SortItems? For owner drawn lists where sometimes, text is not avaible! So for anything text oriented ListView_SortItemsEx would be the right macro to use.
Here is the working code for listview sorting and callback.
and for the callback....
As simple as that! Thnx for the help.
Here is the working code for listview sorting and callback.
if (pnm->hdr.code == LVN_COLUMNCLICK) { //DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_SORT), // NULL, (DLGPROC)SortDlgProc, 0); if(nSortDir[pnm->iSubItem]) nSortDir[pnm->iSubItem] = false; else nSortDir[pnm->iSubItem] = true; ListView_SortItemsEx(hList, ListViewCompareProc, pnm->iSubItem); nItem1=0; return 0; }
and for the callback....
int CALLBACK ListViewCompareProc (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { staticchar szBuf1[30], szBuf2[30]; // Determine the items that we are comparing. //........................................... // Retrieve the item text so we can compare it. //............................................. ListView_GetItemText(hList, lParam1, lParamSort, szBuf1, sizeof(szBuf1)); ListView_GetItemText(hList, lParam2, lParamSort, szBuf2, sizeof(szBuf2)); // Return the comparison results. //............................... if (nSortDir[lParamSort] ) // ACENDING ORDER return(strcmp(szBuf1, szBuf2) * -1); else return(strcmp(szBuf1, szBuf2)); }
As simple as that! Thnx for the help.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
![]() |
Similar Threads
- Creating a coundown timer in a form (VB.NET)
- from list view to label (Visual Basic 4 / 5 / 6)
- help with list view (C++)
- List View save data from it to mysql (VB.NET)
- List View (like in VB) in Java? (Java)
Other Threads in the C Forum
- Previous Thread: HTTPS Libraries
- Next Thread: Reading Keystates
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





