Question

I have a variable-height owner draw ListBox control. When I call AddString, a MeasureItem message is sent asking me to tell Windows the size of the item. The problem is, the code hasn't had a chance to call SetItemData yet, so the code can't determine the size yet.

I've tried calling SetRedraw(FALSE) hoping that would postpone the measure request, but no luck.

So how can I add an item to a CListBox and get the Item Data passed in at the same time? I've tried with and without LBS_HASSTRINGS. Not sure what else to try.

Was it helpful?

Solution

If your list box control does not have the LBS_HASSTRINGS style, the pointer argument to AddString() is not considered as a string but as item data (see the remarks section in the documentation for LB_ADDSTRING).

Therefore, if your list items consist in both a string and some item data, you can wrap these two pieces of information in a structure or a class and pass a pointer to that wrapper to AddString().

The upside is that both the string and the item data will be available by the time MeasureItem() is called. The downside is that you will have to implement CompareItem() if your list is sorted, and to free the wrappers both when items are deleted and before the list box control is destroyed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top