Solved

How to use ListCtrl's GetItem

Posted on 1998-09-03
3
569 Views
Last Modified: 2013-11-20
I need to retrieve the pzsText from a listctrl item.
I only know its iItem value.  The code below i tried keep crashing.

LV_ITEM lv ;
lv.mask = LVIF_TEXT ;
lv.iItem = 10 ; // e.g. i need the pszText from item No.10
m_listCtrl.GetItem(&lv);

CString psz = lv.pszText ;

Any suggestion?
0
Comment
Question by:leowlf
[X]
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
  • 2
3 Comments
 

Author Comment

by:leowlf
ID: 1321658
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1321659
Answer in my comment below (takes me time to type)
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1321660
Basically your code is almost right, but there's a couple of problems:

1. The main problem is it's upto you to supply the buffer pointed to by LV_ITEM (and it's size).  The reason for the crash is that you don't supply the buffer, and where-ever lv.pszText happens to get point (probably NULL) will get overwritten.

2. You really should explicity intiialize the iSubItem member of lv to 0 (or a subitem).  The compiler will not necessarily do this for you

3. It would probably also be a good idea to check the return value of GetItem

The fixed code is then
char szBuffer[256] ;
LV_ITEM lv ;
lv.mask = LVIF_TEXT ;
lv.iItem = 10 ;
lv.iSubItem = 0 ;
lv.pszText = szBuffer ;
lv.cchTextMax = sizeof szBuffer ;
CString strValue ;
if ( m_listCtrl.GetItem(&lv) )
{
   strValue = szBuffer ;
}


One final point, if you are only ever retreiving the text, have you considered using GetItemText member of CListCtrl, example:

CString strValue ;
strValue = m_listCtrl.GetItemText( 10, 0 ) ; // first param is iItem, second is iSubItem
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 72
withoutTen challenge 14 142
matchUp  challenge 9 134
Problem to App 4 145
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

710 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