Solved

CListCtrl problem

Posted on 2001-08-03
8
438 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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
 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.

832 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