?
Solved

Problem with Sorting in CListCtrl

Posted on 2003-03-26
4
Medium Priority
?
881 Views
Last Modified: 2013-11-20
I am new to the VC++ environment. I am trying to build a small application which uses the CListCtrl class. Everything seems to be working fine. But am faced with a slight problem while using the SortItems method. i have 6 colums - data - id - email - data - name - filename
i have used the "OnColumnclick......" command to  sort my list when the tab in each column is clicked based on the column index. The code is as follows:
void CArchivedView::OnColumnclickInfolist(NMHDR* pNMHDR, LRESULT* pResult)
{
     int ret ;
     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
     
     switch(pNMListView->iSubItem)
     {
            case 0:
                    m_infolist.SortItems(&CVSortByDT, (LPARAM)&m_infolist) ;
                    break ;
                   ......

          case 5:
                    m_infolist.SortItems(&CVSortByFileName, (LPARAM)&m_infolist) ;
                                  break ;
     }

     *pResult = 0;
}
m_infolist is the member of the CArchivedview class.

by callback function for say the case 5 is:
int CALLBACK CVSortByFileName(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
     CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
     CString str1 = pListCtrl->GetItemText(lParam1,5) ;
     AfxMessageBox(str1) ;
     CString str2 = pListCtrl->GetItemText(lParam2,5) ;
     AfxMessageBox(str2) ;

     int ret = str1.CompareNoCase(str2) ;
     return ret ;
}

this is where the problem is - this function doenst seem to work. Am using the message box to print the string of each parameter. It always prints the first entry in the column alone(all the time). I'm not sure whats wrong. I tokk this piece of code from the MSDN as is. Can anyone help with this?
Thanx.
0
Comment
Question by:ajaysusarla
[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
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 100 total points
ID: 8217539
Hi ajaysusarla,

the problem here is that CListCtrl::SortItems() sends the
LVM_SORTITEMS to the message box. The message handler for
this message uses the passed callback function to sort.
The 'lParam's passed to the function are the user-defined
data associated with the item (with CListCtrl::SetItemData()),
not the item's indicies.

Now you have two possibilities:
1. you find a way to sort the list only through access to the
user-defined data.

2. you use the LVM_SORTITEMSEX message which does exactly what
you want ... it passes the HWND of the list-control and the indicies of the two items to compare.
Problem with LVM_SORTITEMSEX is that it is only available
with 'Windows 2000, Windows NT 4.0 with Internet Explorer 5, Windows 98'

In both cases I would suggest that you pass the clicked column
ID as lParamSort instead of a pointer to the list.
So you can have one single callback function which knows
by this parameter which column is used to sort.

I would strictly suggest not to pass the list control pointer
to the sort function in case of LVM_SORTITEMS and to iterate
through the lists items to find the item's indicies from its
user-defined data ... this may decrease performance very much.

hope that helps,

ZOPPO
0
 

Author Comment

by:ajaysusarla
ID: 8222679
Hey thanx Zoppo,
That Worked! Thanx a million (again).

ajaysusarla
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 8223138
Your welcome,

have a nice day,

regards,

ZOPPO
0
 

Author Comment

by:ajaysusarla
ID: 8223274
I followed Zoppos instructions with a little bit of improvisation to suit my app. And it worked.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

800 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