Solved

How to use ListCtrl's GetItem

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

9 Experts available now in Live!

Get 1:1 Help Now