Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CListCtrl sorting with sortItem method...

Posted on 1999-09-07
4
Medium Priority
?
993 Views
Last Modified: 2012-05-05
I would like to use the sortItem method provided with the CListCtrl. But when I trace inside my comparision function:
"int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)"

"lParam1" and "lParam2" are null. Maybe the lParamSort should be used ? Can anyone give me a sample of code on how using this function ? Thanks.
0
Comment
Question by:wooper
[X]
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
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 2030074
Hi wooper,

The sample code in the MSDN help for CListCtrl::SortItems() should not work. Cause:

The CListCtrl::SortItems() function uses the LVM_SORTITEMS message to sort the list items. Sorting with this message passes the user defined item data (to set with CListCtrl::SetItemData) to the given compare function.
There also exists a LVM_SORTITEMSEX message, which sorts the items by passing the item id's to the given compare function. There is no CListCtrl-wrapper function for this message, but you can use ListView_SortItemsEx() macro to sort the list.

hope that helps,

ZOPPO
0
 

Author Comment

by:wooper
ID: 2030276
Hmm...  the Visual v4.0 doesn't reconize the "ListView_SortItemsEx()" macro.
Moreover, when I want to use the "ListView_SortItems" macro instead of the "ListView_SortItemsEx()", the compiler sent me a strange error message:

"error C2660: 'SendMessageA' : function does not take 4 parameters"

It's a non sense ?!?

The macro seems to be already there, so...

Here is the code I've used to call it:

HWND a = m_list.GetSafeHwnd();
PFNLVCOMPARE b = CompareFunc;
LPARAM c = dwData;
ListView_SortItems(a, b, c);

What's wrong ?
0
 
LVL 3

Accepted Solution

by:
danny_pav earned 200 total points
ID: 2030347
Every time you add an item to the list control, set its item data (CListCtrl::SetItemData(int, DWORD)).  Now when you go to sort the items, lparam1 is the Item Data of item 1 and lParam2 is Item Data of item 2.  

So big deal.  What do I do?

Here's something I do.  When I insert a record, I insert a pointer to an instance of the record.

void CDlgX::InsertRec(const CSomeRec& Rec)
{
  int nItem = m_ListCtrl.AddItem(....);
....
  m_ListCtrl.SetItemData(nItem, reinterpret_cast<DWORD>(new CSomeRec(Rec)));
}

now of course, I have to delete the item when I remove all elements or memory will leak.

void CDlgX::DeleteItem(int nIndex)
{
DWORD dw = m_ListCtrl.GetItemData(nIndex);
m_ListCtrl.RemoveItem(nIndex);
CSomeRec * pRec = reinterpret_cast<CSomeRec *>(dw);
delete pRec;
}

OK, so I just added some extra stuff but how do I benefit?

In the compare function, the lParams given can be used to determine the data:

int CompareFunc(LPARAM lp1, LPARAM lp2, LPARAM lpSort)
{
CSomeRec * pRec1 = reinterpret_cast<CSomeRec *>(lp1);
CSomeRec * pRec2 = reinterpret_cast<CSomeRec *>(lp2);

return pRec1->Compare(*pRec2);
}
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2030672
Sorry, found out that LVM_SORTITEMSEX requires InternetExplore > 5.0.

The error with the macro seems to be that the macro uses SendMessage instead of ::SendMessage.

So danny_pav's answer is correct.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

715 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