Solved

CListCtrl, LVS_SORTASCENDING and LVITEM.lParam

Posted on 2000-03-21
2
1,544 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 52
deburging in oracle form 12 76
bigDiff challenge 17 75
twoTwo  challenge 35 86
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now