Link to home
Start Free TrialLog in
Avatar of kitwei
kitwei

asked on

CListCtrl problem

Hi, I got a problem here. Seems like CListCtrl does not accept duplicate data in the second column.

typedef struct
{     CString     myName;                        
     CString     myIP;} NameIPPair;

NameIPPair     myList[3];
CListCtrl myCListCtrl;

     for (i = 0; i < nItem; i++) {
          LV_ITEM lvItem;
          LV_ITEM lvSubItem;
          lvItem.mask = LVIF_TEXT;
          lvItem.iItem = i;
          lvItem.iSubItem = 0;
          lvItem.pszText = myList[i].myName.GetBuffer(myList[i].myName.GetLength());
          myCListCtrl.InsertItem(&lvItem);

          lvSubItem.mask = LVIF_TEXT;
          lvSubItem.iItem = i;
          lvSubItem.iSubItem = 1;
          lvSubItem.pszText = myList[i].myIP.GetBuffer(myList[i].myIP.GetLength());
          myCListCtrl.SetItem(&lvSubItem);
     }

Ok, in the myList array, I have data like this, I stepped through the program and made sure the array has data.
    myName    myIP
0:   haha     abc
1:   hehe     hoho
2:   hehe     hoho

When the dialog is displayed, the listctrl displays

0:   haha     abc
1:   hehe    
2:   hehe     hoho

The first hoho is gone, I have no idea why? Anything wrong with the for loop? If nothing is duplicate, everything will show up just fine, the thing is I want to display duplicate data.
Avatar of migel
migel

Hi!
you do not release buffer of the string
also you can avoid using GetBuffer by implement  two typecast:
lvSubItem.pszText = (LPSTR)(LPCSTR)myList[i].myIP;
Avatar of DanRollins
Just use:

for (i = 0; i < nItem; i++) {
   myCListCtrl.InsertItem(j, myList[i].myName );
   myCListCtrl.SetItemText(j, 1, myList[i].myIP );
}

CString::GetBuffer is almost never needed (and when you use it, you must be certain to use ReleaseBuffer).  Don't use it here.  Let InsertItem and SetItemText do the standard handling needed for passing a CString into the control.

-- Dan

         
Avatar of kitwei

ASKER

(LPSTR)(LPCSTR) didn't work for me, and I will try the other InsertItem tomorrow, thanks!
Avatar of kitwei

ASKER

I got the same error, duplicate data fr different sources, and only the second column does not display the first duplicate data.
Is there something in the resource dialog I should check?
ASKER CERTIFIED SOLUTION
Avatar of hoooi
hoooi

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
good one hoooi!

kitwiel,
You can either make the list unsorted, or use this modified code:

for (i = 0; i < nItem; i++) {
  int nIdx= myCListCtrl.InsertItem(j, myList[i].myName );
  myCListCtrl.SetItemText(nIdx, 1, myList[i].myIP );
}

This version takes into account the fact that when the list gets sorted, the newly-inserted item may end up at any location in the list.

-- Dan
Avatar of kitwei

ASKER

Thanks Dan for your suggestion, but hoooi made the right assumption and that fixed it. I don't know why I checked the box in the first place and that wasted a few days of my time...
Quite alright.  u should award insight.

-- Dan