Solved

ListView Sorting - CompareFunction

Posted on 2001-07-26
9
705 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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