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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ClistCtrl More help needed

ClistCtrl More help needed

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionalgorithms
7 Posts 3 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.
  • H Offline
    H Offline
    Halawlaws
    wrote on last edited by
    #1

    I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)

    J D 2 Replies Last reply
    0
    • H Halawlaws

      I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)

      J Offline
      J Offline
      Jose Lamas Rios
      wrote on last edited by
      #2

      Take a look at CListCtrl::SortItems[^] If you pass both the list control pointer and a column index in some struct as the dwData parameter to SortItems, your compare function can obtain the text for the appropriate column and use it for the comparison.

      struct SORT_DATA
      {
         CListCtrl* pList;
         int nColumn;
      }
       
      static int CALLBACK
      MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
      {
         SORT_DATA* pData = (SORT_DATA*) lParamSort;
         CListCtrl* pListCtrl = pData->pList;
         int nColumn = pData->nColumn
       
         CString strItem1 = pListCtrl->GetItemText(lParam1, nColumn);
         CString strItem2 = pListCtrl->GetItemText(lParam2, nColumn);
       
         return strcmp(strItem1, strItem2);
      }
       
      void CListCtrl_SortItems(CListCtrl* pList, int nColumn)
      {
         SORT_DATA data = {pList, nColumn};
       
         // Sort the list view items using my callback procedure.
         pList->SortItems(&MyCompareProc, (LPARAM) &data);
      }

      You could easily extend this to allow for sorting order (ascending/descending), sort on multiple columns, etc. Hope that helps, -- jlr http://jlamas.blogspot.com/[^]

      H 1 Reply Last reply
      0
      • H Halawlaws

        I have a CListCtrl with 3 columns I am using this function to know when someone clicks on the column button void OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult) the problem is it is sorting the list according to the first column no matter what column button i press what sshould i do to make it sort according to the column button i press? In other word how can i know which column button has been pressed? /\|-||\/|/\|)

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

        The way I've handled this in the past was something like:

        BEGIN_MESSAGE_MAP(CMyView, CListView)
        //{{AFX_MSG_MAP(CMyView)
        ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclick)
        //}}AFX_MSG_MAP
        END_MESSAGE_MAP()

        int CALLBACK CMyView::CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM )
        {
        int nReturn = 0;
        CInfo *pInfo1,
        *pInfo2,
        *pTemp;

        pInfo1 = (CInfo \*) lParam1;
        pInfo2 = (CInfo \*) lParam2;
        
        if (true == m\_bAscending)
        {
            pTemp  = pInfo1;
            pInfo1 = pInfo2;
            pInfo2 = pTemp;
        }
        
        switch (m\_nSortColumn)
        {
            // name
            case 0: 
                nReturn = pInfo1->m\_strName.Compare(pInfo2->m\_strName);            
                break;
        
            // USAG #
            case 1:
                nReturn = pInfo1->m\_lUSANumber - pInfo2->m\_lUSANumber;
                break;
        
            // Age
            case 2:
                if (pInfo1->m\_timeBirthdate < pInfo2->m\_timeBirthdate)
                    nReturn = -1;
                else
                    nReturn = 1;
        
                break;
        }
        
        return (nReturn);
        

        }

        void CMyView::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult)
        {
        HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
        CListCtrl &ctrlList = GetListCtrl();

        m\_bAscending = ! m\_bAscending;
        
        **m\_nSortColumn = phdn->iItem;**
        
        ctrlList.SortItems(CompareFunc, 0);
        
        \*pResult = 0;
        

        }


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

        H 1 Reply Last reply
        0
        • J Jose Lamas Rios

          Take a look at CListCtrl::SortItems[^] If you pass both the list control pointer and a column index in some struct as the dwData parameter to SortItems, your compare function can obtain the text for the appropriate column and use it for the comparison.

          struct SORT_DATA
          {
             CListCtrl* pList;
             int nColumn;
          }
           
          static int CALLBACK
          MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
          {
             SORT_DATA* pData = (SORT_DATA*) lParamSort;
             CListCtrl* pListCtrl = pData->pList;
             int nColumn = pData->nColumn
           
             CString strItem1 = pListCtrl->GetItemText(lParam1, nColumn);
             CString strItem2 = pListCtrl->GetItemText(lParam2, nColumn);
           
             return strcmp(strItem1, strItem2);
          }
           
          void CListCtrl_SortItems(CListCtrl* pList, int nColumn)
          {
             SORT_DATA data = {pList, nColumn};
           
             // Sort the list view items using my callback procedure.
             pList->SortItems(&MyCompareProc, (LPARAM) &data);
          }

          You could easily extend this to allow for sorting order (ascending/descending), sort on multiple columns, etc. Hope that helps, -- jlr http://jlamas.blogspot.com/[^]

          H Offline
          H Offline
          Halawlaws
          wrote on last edited by
          #4

          refer to this link to know why this sortitem() doesn't work anyway /\|-||\/|/\|)

          J 1 Reply Last reply
          0
          • D David Crow

            The way I've handled this in the past was something like:

            BEGIN_MESSAGE_MAP(CMyView, CListView)
            //{{AFX_MSG_MAP(CMyView)
            ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclick)
            //}}AFX_MSG_MAP
            END_MESSAGE_MAP()

            int CALLBACK CMyView::CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM )
            {
            int nReturn = 0;
            CInfo *pInfo1,
            *pInfo2,
            *pTemp;

            pInfo1 = (CInfo \*) lParam1;
            pInfo2 = (CInfo \*) lParam2;
            
            if (true == m\_bAscending)
            {
                pTemp  = pInfo1;
                pInfo1 = pInfo2;
                pInfo2 = pTemp;
            }
            
            switch (m\_nSortColumn)
            {
                // name
                case 0: 
                    nReturn = pInfo1->m\_strName.Compare(pInfo2->m\_strName);            
                    break;
            
                // USAG #
                case 1:
                    nReturn = pInfo1->m\_lUSANumber - pInfo2->m\_lUSANumber;
                    break;
            
                // Age
                case 2:
                    if (pInfo1->m\_timeBirthdate < pInfo2->m\_timeBirthdate)
                        nReturn = -1;
                    else
                        nReturn = 1;
            
                    break;
            }
            
            return (nReturn);
            

            }

            void CMyView::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult)
            {
            HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
            CListCtrl &ctrlList = GetListCtrl();

            m\_bAscending = ! m\_bAscending;
            
            **m\_nSortColumn = phdn->iItem;**
            
            ctrlList.SortItems(CompareFunc, 0);
            
            \*pResult = 0;
            

            }


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

            H Offline
            H Offline
            Halawlaws
            wrote on last edited by
            #5

            thx man /\|-||\/|/\|)

            1 Reply Last reply
            0
            • H Halawlaws

              refer to this link to know why this sortitem() doesn't work anyway /\|-||\/|/\|)

              J Offline
              J Offline
              Jose Lamas Rios
              wrote on last edited by
              #6

              Halawlaws wrote: refer to this link to know why this sortitem() doesn't work anyway Hmm.. yep. You are right. But it's easy to fix, isn't it? You just need to set lParamData (while inserting items) with a pointer to the object from which you obtained the strings, and modify the compare function accordingly. -- jlr http://jlamas.blogspot.com/[^]

              H 1 Reply Last reply
              0
              • J Jose Lamas Rios

                Halawlaws wrote: refer to this link to know why this sortitem() doesn't work anyway Hmm.. yep. You are right. But it's easy to fix, isn't it? You just need to set lParamData (while inserting items) with a pointer to the object from which you obtained the strings, and modify the compare function accordingly. -- jlr http://jlamas.blogspot.com/[^]

                H Offline
                H Offline
                Halawlaws
                wrote on last edited by
                #7

                ok i ll try that thx /\|-||\/|/\|)

                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