Solved

CListCtrl problem

Posted on 2001-08-03
8
435 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

20 Experts available now in Live!

Get 1:1 Help Now