CListCtrl problem

Posted on 2001-08-03
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());

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

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.
Question by:kitwei
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

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

Expert Comment

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


Author Comment

ID: 6358293
(LPSTR)(LPCSTR) didn't work for me, and I will try the other InsertItem tomorrow, thanks!
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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?

Accepted Solution

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
LVL 49

Expert Comment

ID: 6360915
good one hoooi!

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

Author Comment

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...
LVL 49

Expert Comment

ID: 6362258
Quite alright.  u should award insight.

-- Dan

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 169
Separate files macro - Errors on larger data set 18 73
deburging in oracle form 12 109
abstract class with all non abstract mentods 6 84
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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

756 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