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.Ge tLength()) ;
myCListCtrl.InsertItem(&lv Item);
lvSubItem.mask = LVIF_TEXT;
lvSubItem.iItem = i;
lvSubItem.iSubItem = 1;
lvSubItem.pszText = myList[i].myIP.GetBuffer(m yList[i].m yIP.GetLen gth());
myCListCtrl.SetItem(&lvSub Item);
}
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.
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
myCListCtrl.InsertItem(&lv
lvSubItem.mask = LVIF_TEXT;
lvSubItem.iItem = i;
lvSubItem.iSubItem = 1;
lvSubItem.pszText = myList[i].myIP.GetBuffer(m
myCListCtrl.SetItem(&lvSub
}
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.
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
for (i = 0; i < nItem; i++) {
myCListCtrl.InsertItem(j, myList[i].myName );
myCListCtrl.SetItemText(j,
}
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
ASKER
(LPSTR)(LPCSTR) didn't work for me, and I will try the other InsertItem tomorrow, thanks!
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?
Is there something in the resource dialog I should check?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(nI dx, 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
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(nI
}
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
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
-- Dan
you do not release buffer of the string
also you can avoid using GetBuffer by implement two typecast:
lvSubItem.pszText = (LPSTR)(LPCSTR)myList[i].m