Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

CListCtrl.SortItems question...

Posted on 1998-10-20
1
376 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

790 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