Solved

CListCtrl problem

Posted on 2001-08-03
8
437 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:kitwei
8 Comments
 
LVL 12

Expert Comment

by:migel
ID: 6350931
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;
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6352410
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

         
0
 

Author Comment

by:kitwei
ID: 6358293
(LPSTR)(LPCSTR) didn't work for me, and I will try the other InsertItem tomorrow, thanks!
0
 

Author Comment

by:kitwei
ID: 6360288
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
hoooi earned 100 total points
ID: 6360320
You must have use Sort:Ascending or Descending for the control, use None instead and that should fix it.
Good luck
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6360915
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
0
 

Author Comment

by:kitwei
ID: 6361280
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...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6362258
Quite alright.  u should award insight.

-- Dan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 91
CRON statement to run every 3 minutes except on Monday morning 12am to 2am 7 89
sameEnds challenge 3 133
Path of Workbook 3 59
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

11 Experts available now in Live!

Get 1:1 Help Now