Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

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

C++
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?

Quy
Posted
Updated 7-Jul-11 15:30pm
v2

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

m_lstBox.LockWindowUpdate();
int idx = m_lstBox.AddString(strTxt);
m_lstBox.SetItemDataPtr(idx, pData);
m_lstBox.UnlockWindowUpdate();
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jul-11 10:23am    
Excellent solution. I forgot about that.
quyps 11-Jul-11 22:06pm    
I totally agree with Marcus. 5*!
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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900