Solved

ListView Sorting - CompareFunction

Posted on 2001-07-26
9
708 Views
Last Modified: 2013-12-03
Hi...
I would liekt to sor the listview according to column which was pressed.
Everything is OK until the
CompareFun(LPARAM lParam1,LPARAM lParam2,LPARAM sortParm)
is hit. Neither of the LPARAM 1 or 2 pass what they suposed to. They pass a value of 0 for both and sortParam
passes the column which was pressed. Below is my source.

ItemStruct *pItem1    = (ItemStruct*) lParam1;
ItemStruct *pItem2    = (ItemStruct*) lParam2;
After this assignment is done both of these are NULL
ie: pItem1 , pItem2.

Can someone take a look and tell me what is wrong.

Thank you in Advance.
Kind Regards Paul
---------------------------------------------------------
typedef struct
{
  int nItemNo;
  char strName[110];
}ItemStruct;

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
  int result;
  ItemStruct *pItem1    = (ItemStruct*) lParam1;
  ItemStruct *pItem2    = (ItemStruct*) lParam2;
  if(pItem1 ==  NULL || pItem2 == NULL)
     return 0;
  switch(lParamSort)
  {
    case 0:
      if(pItem1->nItemNo < pItem2->nItemNo)
         result = -1;
      else
      if(pItem1->nItemNo == pItem2->nItemNo)
         result = 0;
      else
         result = 1;
      break;

      case 1:
        result = strcmp(pItem1->strName,pItem2->strName);
        break;

      default:
        result = 0;
        break;
  }
  return result;
}

void OnColumnclickList1(HWND hWnd,LPARAM lParam)
{
    HWND lWnd;
    int nColumn;
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)lParam;
     lWnd = GetDlgItem(hWnd,ID_SEARCHLIST);
    nColumn = pNMListView->iSubItem;
    ListView_SortItems(lWnd,CompareFunc,nColumn);
}

LRESULT SListViewNotify(HWND hWnd,LPARAM lParam)
{
   LPNMHDR  lpnmh = (LPNMHDR) lParam;
   if(search_set == 0)
      return 0;
   switch(lpnmh->code)
   {
      case LVN_COLUMNCLICK:
         OnColumnclickList1(hWnd,lParam);
         break;
   }
   return 0;
}

---------------------------------------------------------
0
Comment
Question by:pawel3
9 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 200 total points
ID: 6324782
<<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.>>

You have to explicitely assign a certain value to lParam for each listview item when you create a listview.

By default, lParam contains zero.

lParam is among other members in LVITEM structure.

0
 
LVL 5

Expert Comment

by:robpitt
ID: 6324802
Question:
In your OnColumnclickList1() what is the ID_SEARCHLIST thing all about?


Also be sure to check that your actually setting the lParam correctly in the first place. I.e. check that the LVITEM mask member includes LVIF_PARAM as well as setting the lParam member to your structure address.

Rob
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6326006
PS Ignore my first question about ID_SEARCHLIST - I was just being dim.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Expert Comment

by:AKRover
ID: 6372658
can u post the code that uses LVM_INSERTITEM?
0
 

Author Comment

by:pawel3
ID: 6681287
thank you NickRepin
you suggestion worked fine
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 6682531
if so then could you accept my comment as an answer?
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

763 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