Solved

CListCtrl, LVS_SORTASCENDING and LVITEM.lParam

Posted on 2000-03-21
2
1,566 Views
Last Modified: 2013-11-20
I am using an Explorer-type application generated in Visual C++ 6.0 and I'm trying to populate the CListCtrl depending on values selected in the CTreeCtrl.  I am storing a pointer to an object in the ItemData of each CListCtrl entry.

My problem is that when using the LVS_SORTASCENDING style for the list control, the labels seem to get sorted, but the underlying LVITEMs don't seem to be following suit. If the item sort order differs from my item insertion order, then the ItemData for the item is null.

I wish to use the CListCtrl's default behaviour of sorting based on the ItemText.

I would assume that the CListCtrl actually sorts (and moves around) individual LVITEM structs under the covers (I think the CTreeCtrl does this) but the behaviour of the CListCtrl seems to indicate that LVITEMs and the item labels themselves are entirely different creatures.

The Insertion and Retrieval routines are below:

void CAdminClientView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
   CAdminClientDoc*   pDoc = GetDocument();
   CListCtrl&   ctlList = GetListCtrl();

   LISTINFOVECTOR::iterator it;

   // LISTINFOVECTOR is a vector<listInfo>
   //  typedef struct listInfoStruct {
   //      CString strLabel;
   //      LPARAM lParam;   // a pointer to a CKindOfRecord object
   //   } listInfo;

   ctlList.DeleteAllItems();

   LISTINFOVECTOR* pLst = pDoc->GetListItems();
      
   int i = 0;
   for ( it = pLst->begin(); it < pLst->end(); it++)
   {
      int rc = ctlList.InsertItem(i, (LPCTSTR)(it->strLabel));
      rc = ctlList.SetItemData(i, it->lParam);

      // following line just to verify values in debugger
      DWORD res = ctlList.GetItemData(i);

      i++;
   }

The retrieval routine I'm using to retrieve the ItemData goes something like this:

   CAdminClientDoc* pDoc = GetDocument();
   CListCtrl& ctlList = GetListCtrl();
   void* pRec = 0;

   // following algorithm shamelessly pilfered from MSDN
   POSITION pos = ctlList.GetFirstSelectedItemPosition();
   if (pos == NULL)
   {
      TRACE0("No items were selected!\n");
   }
   else
   {
      while (pos)
      {
         int nItem = ctlList.GetNextSelectedItem(pos);
         TRACE1("Item %d was selected!\n", nItem);

         CString strTest = ctlList.GetItemText( nItem, 0 );

         // following lines expanded for walking through debugger with
         DWORD temp = ctlList.GetItemData(nItem);
         pRec = (void*)temp;
         CKindOfRecord* pWorkRec = (CKindOfRecord*)pRec;
      }
}

Does storing ItemData throw off the sort routine? Will I have to implement a callback sort function? Will I have to manually intercept an LVM_SORTITEMS message?

Thanks,
0
Comment
Question by:ayang_ca
2 Comments
 
LVL 2

Accepted Solution

by:
paulburns earned 100 total points
ID: 2642218
I think your problem is in using 'i' in setting the item data...

int rc = ctlList.InsertItem(i, (LPCTSTR)(it->strLabel));
rc = ctlList.SetItemData(i, it->lParam);

Change this to...

int rc = ctlList.InsertItem(i, (LPCTSTR)(it->strLabel));
if (rc != -1)
   ctlList.SetItemData(rc, it->lParam);


The reason is that when you insert the item the list control will sort it and possibly change its index, therefore 'i' will no longer be its index.
0
 

Author Comment

by:ayang_ca
ID: 2644755
Excellent! Just what I needed. Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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