Click here to Skip to main content
12,889,951 members (43,712 online)
Rate this:
Please Sign up or sign in to vote.

I implement a list box which has items with variable height, to calculate height of item i need to obtain data attached with it in MeasureItem().

Here is part of code inserting item in list box

INT idx = m_lstBox.AddString(strTxt);
m_lstBox.SetItemDataPtr(idx, pData);

Problem is that MeasureItem() function is called when the string is added, and at that time, data pointer is still not be set so i got bad pointer as result of GetItemDataPtr inside MeasureItem().

How can I set data pointer before MeasureItem call?

Posted 7-Jul-11 15:29pm
Updated 7-Jul-11 15:30pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can lock the window before adding items to it so that it will not redraw until your addition of items is complete:

int idx = m_lstBox.AddString(strTxt);
m_lstBox.SetItemDataPtr(idx, pData);
Marcus Kramer 8-Jul-11 10:23am
Excellent solution. I forgot about that.
quyps 11-Jul-11 22:06pm
I totally agree with Marcus. 5*!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Are you sure you don't want to finish adding all the items, then perform an UpdateData so that the list item actually gets created first before you try to measure it?
I'm not 100% this would work as I haven't been in MFC for the last 6 months, but this did ring a bell.
quyps 8-Jul-11 0:01am
thanks Marcus! I have had a same thought. But there is another issue: how to know in MeasureItem() that it is called from AddItem() or UpdateData.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 8 Jul 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100