Link to home
Start Free TrialLog in
Avatar of ayang_ca
ayang_ca

asked on

CListCtrl, LVS_SORTASCENDING and LVITEM.lParam

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,
ASKER CERTIFIED SOLUTION
Avatar of paulburns
paulburns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ayang_ca
ayang_ca

ASKER

Excellent! Just what I needed. Thanks.