Solved

CListCtrl.SortItems question...

Posted on 1998-10-20
1
367 Views
Last Modified: 2013-11-20
CListCtrl.SortItems(CompareFunc, dwData)
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lpara);

I use debugger to step inside the 'CompareFunc' and find out
lParam1 and lParam2 are both 0!!!!! However, lpara has the correct value. WHY????
0
Comment
Question by:stephenkong
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 0 total points
ID: 1323605
lParam1 and lParam2 are the item data associated with the two items being compared.

If you don't set item data then it's zero

To set item data with an item - use SetItemData member of CListCtrl.

The normal way way to do this is
1. In dwData pass a "pseudo" this ptr to the class
SortItems( CompareFunc, (LPARAM)this ) ;

2. In CompareFunc convert this back to a CListCtrl *, or a ptr to your derived class
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lpara)
{
 CListCtrl * pList = (CListCtrl *)lpara ;
}

3. To get non-zero item data
EITHER
(a) Set the row-id as item data for each item in the list before doing SortItems - then in CompareFunc you the item data as a param to GetItemText
OR
(b) Set the item data to point to some structure (you allocate) containing comparison data which the sort function uses

4. BTW if you want CompareFuncs to be a class member, make it a static member


Here's an example - that uses method 3(b).  Previous to this I have set item data with a record id for an AAInfo record (which is my data structure).  GetAA is a function I wrote to get an AAInfo record.  m_nSortColumn and m_bSortReverse are an int and BOOL class member

void CTComply::SortList()
{
      CWaitCursor oWait ;
      m_list.SortItems( CTComply::CompareFunc, (DWORD)this ) ;
}

nt CALLBACK CTComply::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
      CTComply * pComply = (CTComply *)lParamSort ;
      ASSERT_VALID( pComply ) ;

      AAInfo oInfo1 ;
      AAInfo oInfo2 ;

      // get 2 items
      VERIFY( GetAA( lParam1, oInfo1 ) ) ;
      VERIFY( GetAA( lParam2, oInfo2 ) ) ;


      // now get 2 strings
      CString str1 ;
      CString str2 ;

      switch ( pComply->m_nSortColumn )
      {
      default :
      case 0 :
            str1 = oInfo1.m_strManufacturer ;
            str2 = oInfo2.m_strManufacturer ;
            break ;

      case 1 :
            str1 = oInfo1.m_strProduct ;
            str2 = oInfo2.m_strProduct ;
            break ;

      case 2 :
            str1 = oInfo1.m_strVersion ;
            str2 = oInfo2.m_strVersion ;
            break ;

      case 3 :
            str1 = oInfo1.m_strProductType ;
            str2 = oInfo2.m_strProductType ;
            break ;

      case 4 :
            str1 = oInfo1.GetStatus() ;
            str2 = oInfo2.GetStatus() ;
            break ;
      }


      // do comparison
      int nn = 0 ;

      if ( str1 < str2 ) nn = -1 ;
      if ( str1 > str2 ) nn = 1 ;

      if ( pComply->m_bSortReverse )
      {
            nn = 0 - nn ;
      }

      return nn ;
}

void CTComply::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

      int nNewHeader = pNMListView->iSubItem ;

      if ( nNewHeader == m_nSortColumn )
      {
            m_bSortReverse = !m_bSortReverse ;
      }
      m_nSortColumn = nNewHeader ;
      SortList() ;

      *pResult = 0;
}





0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question