Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ListView_SortItems question

ListView_SortItems question

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomhelp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Wright
    wrote on last edited by
    #1

    I'm having a bit of a problem getting the handle to my listview. Can anyone suggest how I would pass this so that I can get the listview text and compare? Thanks Tom Wright tawright915@yahoo.com

    C 1 Reply Last reply
    0
    • T Tom Wright

      I'm having a bit of a problem getting the handle to my listview. Can anyone suggest how I would pass this so that I can get the listview text and compare? Thanks Tom Wright tawright915@yahoo.com

      C Offline
      C Offline
      ClockDivider
      wrote on last edited by
      #2

      I am not that experienced but as far as i know you have to pass a callback funtion as parameter: i have done it useing mfc so zou have to translate that somewhat, this is the member function i declared as member of my derived listview, this example is used to sort numeric columns: static int CALLBACK CallbackSortNumeric(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort); Then, when you add a column, you have to pass the adress of the function (PFNLVCOMPARE pCALLBACKSORT): this->InsertColumn(nCol, cHead,pCALLBACKSORT,iSortMode,iFormat, nWidth,nSubItem); This way the sorting is managed in CallBackSortNumeric. Hope it helps... Martin...

      T 1 Reply Last reply
      0
      • C ClockDivider

        I am not that experienced but as far as i know you have to pass a callback funtion as parameter: i have done it useing mfc so zou have to translate that somewhat, this is the member function i declared as member of my derived listview, this example is used to sort numeric columns: static int CALLBACK CallbackSortNumeric(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort); Then, when you add a column, you have to pass the adress of the function (PFNLVCOMPARE pCALLBACKSORT): this->InsertColumn(nCol, cHead,pCALLBACKSORT,iSortMode,iFormat, nWidth,nSubItem); This way the sorting is managed in CallBackSortNumeric. Hope it helps... Martin...

        T Offline
        T Offline
        Tom Wright
        wrote on last edited by
        #3

        ClockDivider wrote: I am not that experienced but as far as i know you have to pass a callback funtion as parameter: Yeah I did that. My problem is that in my callback I need to access the window handle and I cannot. I get a compiler error: illegal reference to data member 'FileTransfer::hWnd' in a static member function Here is my callback code: int CALLBACK FileTransfer::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(hWnd, -1, &fi); fi.lParam = lParam2; //nItem2 = ListView_FindItem(hWnd, -1, &fi); return 0; } Thanks Tom Wright tawright915@yahoo.com

        C G D 3 Replies Last reply
        0
        • T Tom Wright

          ClockDivider wrote: I am not that experienced but as far as i know you have to pass a callback funtion as parameter: Yeah I did that. My problem is that in my callback I need to access the window handle and I cannot. I get a compiler error: illegal reference to data member 'FileTransfer::hWnd' in a static member function Here is my callback code: int CALLBACK FileTransfer::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(hWnd, -1, &fi); fi.lParam = lParam2; //nItem2 = ListView_FindItem(hWnd, -1, &fi); return 0; } Thanks Tom Wright tawright915@yahoo.com

          C Offline
          C Offline
          ClockDivider
          wrote on last edited by
          #4

          I am not sure why :-) but here is some code i have in my function that might belong to the problem: int CALLBACK CListCtrlEx::CallbackSortString(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. CListCtrlEx* pListCtrl = (CListCtrlEx*) lParamSort; } Thus you should be able to convert lParamSort to a CWnd* which then provides the propper m_hWnd Is that true?

          T 1 Reply Last reply
          0
          • C ClockDivider

            I am not sure why :-) but here is some code i have in my function that might belong to the problem: int CALLBACK CListCtrlEx::CallbackSortString(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. CListCtrlEx* pListCtrl = (CListCtrlEx*) lParamSort; } Thus you should be able to convert lParamSort to a CWnd* which then provides the propper m_hWnd Is that true?

            T Offline
            T Offline
            Tom Wright
            wrote on last edited by
            #5

            The problem with this is that I'm not using any MFC. Here is my event code: LPNMLISTVIEW lpNmlv = (LPNMLISTVIEW) lParam; case LVN_COLUMNCLICK: if (lpNmlv->hdr.hwndFrom == GetDlgItem(hWnd, IDC_LOCAL_FILELIST)) { ListView_SortItems(lpNmlv->hdr.hwndFrom, ListViewCompareProc,(LPARAM)(lpNmlv)); } if (lpNmlv->hdr.hwndFrom == GetDlgItem(hWnd, IDC_REMOTE_FILELIST)) { HWND RemoteHwnd = GetDlgItem(hWnd, IDC_REMOTE_FILELIST); } return TRUE; int CALLBACK FileTransfer::ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { static LV_FINDINFO fi; static int nItem1, nItem2; static char szBuf1[30], szBuf2[30]; NMLISTVIEW *lpNmlv = (NMLISTVIEW*)lParamSort; // Determine the items that we are comparing. //........................................... fi.flags = LVFI_PARAM; fi.lParam = lParam1; nItem1 = ListView_FindItem(lpNmlv->hdr.hwndFrom, -1, &fi); fi.lParam = lParam2; nItem2 = ListView_FindItem(lpNmlv->hdr.hwndFrom, -1, &fi); // Retrieve the item text so we can compare it. //............................................. ListView_GetItemText(lpNmlv->hdr.hwndFrom, nItem1, lParamSort, szBuf1, sizeof(szBuf1)); ListView_GetItemText(lpNmlv->hdr.hwndFrom, nItem2, lParamSort, szBuf2, sizeof(szBuf2)); // Return the comparison results. //............................... if (lpNmlv) // ACENDING ORDER return(strcmp(szBuf1, szBuf2)); else return(strcmp(szBuf1, szBuf2) * -1); } Thanks Tom Wright tawright915@yahoo.com

            C 1 Reply Last reply
            0
            • T Tom Wright

              The problem with this is that I'm not using any MFC. Here is my event code: LPNMLISTVIEW lpNmlv = (LPNMLISTVIEW) lParam; case LVN_COLUMNCLICK: if (lpNmlv->hdr.hwndFrom == GetDlgItem(hWnd, IDC_LOCAL_FILELIST)) { ListView_SortItems(lpNmlv->hdr.hwndFrom, ListViewCompareProc,(LPARAM)(lpNmlv)); } if (lpNmlv->hdr.hwndFrom == GetDlgItem(hWnd, IDC_REMOTE_FILELIST)) { HWND RemoteHwnd = GetDlgItem(hWnd, IDC_REMOTE_FILELIST); } return TRUE; int CALLBACK FileTransfer::ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { static LV_FINDINFO fi; static int nItem1, nItem2; static char szBuf1[30], szBuf2[30]; NMLISTVIEW *lpNmlv = (NMLISTVIEW*)lParamSort; // Determine the items that we are comparing. //........................................... fi.flags = LVFI_PARAM; fi.lParam = lParam1; nItem1 = ListView_FindItem(lpNmlv->hdr.hwndFrom, -1, &fi); fi.lParam = lParam2; nItem2 = ListView_FindItem(lpNmlv->hdr.hwndFrom, -1, &fi); // Retrieve the item text so we can compare it. //............................................. ListView_GetItemText(lpNmlv->hdr.hwndFrom, nItem1, lParamSort, szBuf1, sizeof(szBuf1)); ListView_GetItemText(lpNmlv->hdr.hwndFrom, nItem2, lParamSort, szBuf2, sizeof(szBuf2)); // Return the comparison results. //............................... if (lpNmlv) // ACENDING ORDER return(strcmp(szBuf1, szBuf2)); else return(strcmp(szBuf1, szBuf2) * -1); } Thanks Tom Wright tawright915@yahoo.com

              C Offline
              C Offline
              ClockDivider
              wrote on last edited by
              #6

              Ummm... Your code compiles here... But you already changed the calling of the ListView_FindItem and ListView_GetItemText, now using the lpNmlv.>hdr.hwndFrom, no? What is the exact compiler error?

              T 1 Reply Last reply
              0
              • C ClockDivider

                Ummm... Your code compiles here... But you already changed the calling of the ListView_FindItem and ListView_GetItemText, now using the lpNmlv.>hdr.hwndFrom, no? What is the exact compiler error?

                T Offline
                T Offline
                Tom Wright
                wrote on last edited by
                #7

                Yeah it compiles fine now. Just still not getting anything. I think I'm not setting my pointers correctly or the NMLISTVIEW struct. You know there aren't very many samples of how to use this out there on the internet. Must be used much. Tom Wright tawright915@yahoo.com

                C 1 Reply Last reply
                0
                • T Tom Wright

                  ClockDivider wrote: I am not that experienced but as far as i know you have to pass a callback funtion as parameter: Yeah I did that. My problem is that in my callback I need to access the window handle and I cannot. I get a compiler error: illegal reference to data member 'FileTransfer::hWnd' in a static member function Here is my callback code: int CALLBACK FileTransfer::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(hWnd, -1, &fi); fi.lParam = lParam2; //nItem2 = ListView_FindItem(hWnd, -1, &fi); return 0; } Thanks Tom Wright tawright915@yahoo.com

                  G Offline
                  G Offline
                  Graham Bradshaw
                  wrote on last edited by
                  #8

                  Tom Wright wrote: My problem is that in my callback I need to access the window handle and I cannot That's what the lParamSort paramter is for. You pass in whatever you want (for examnple, a number or a pointer to a data structure), and the same value is passed to your comparison function. Your comparision function can then interpret that lParam in the right way (as a number or dereference the pointer to get the data structure again), and process it.

                  1 Reply Last reply
                  0
                  • T Tom Wright

                    Yeah it compiles fine now. Just still not getting anything. I think I'm not setting my pointers correctly or the NMLISTVIEW struct. You know there aren't very many samples of how to use this out there on the internet. Must be used much. Tom Wright tawright915@yahoo.com

                    C Offline
                    C Offline
                    ClockDivider
                    wrote on last edited by
                    #9

                    The prob is this in: ListView_GetItemText(lpNmlv->hdr.hwndFrom, lParam2, lParamSort, szBuf2, sizeof(szBuf2)); the parameter lParamsort is wrong here, it should define the column (lpNmlv->iSubItem). AND: Do you to set the LPARAM value when inserting your item? Here is an excerpt from .NET help: "The lParam1 parameter is the 32-bit value associated with the first item being compared, and the lParam2 parameter is the value associated with the second item. These are the values specified in the lParam member of the items’ LVITEM structure when they were inserted into the list." I hade precisely the same problems as you, now that I assured being the item.lPram member valid and changing the wrong parameter value to the item.iSubitem it works... Hope it helps now?...

                    1 Reply Last reply
                    0
                    • T Tom Wright

                      ClockDivider wrote: I am not that experienced but as far as i know you have to pass a callback funtion as parameter: Yeah I did that. My problem is that in my callback I need to access the window handle and I cannot. I get a compiler error: illegal reference to data member 'FileTransfer::hWnd' in a static member function Here is my callback code: int CALLBACK FileTransfer::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(hWnd, -1, &fi); fi.lParam = lParam2; //nItem2 = ListView_FindItem(hWnd, -1, &fi); return 0; } Thanks Tom Wright tawright915@yahoo.com

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      I think you should be using ListView_GetItemData() with lParam1 and lParam2.


                      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups