• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1692
  • Last Modified:


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;


   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);


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");
      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?

1 Solution
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.
ayang_caAuthor Commented:
Excellent! Just what I needed. Thanks.

Featured Post


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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now