Solved

How to use ListCtrl's GetItem

Posted on 1998-09-03
3
554 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
  • 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

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

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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

16 Experts available now in Live!

Get 1:1 Help Now