Solved

How to use ListCtrl's GetItem

Posted on 1998-09-03
3
551 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
Comment Utility
Edited text of question
0
 
LVL 8

Accepted Solution

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

Expert Comment

by:Answers2000
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
Separate files macro - Errors on larger data set 18 56
maxBlock challenge 30 99
MaxSpan challenge 9 65
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

14 Experts available now in Live!

Get 1:1 Help Now