Link to home
Start Free TrialLog in
Avatar of tflex
tflex

asked on

DeleteItem in non-ownerdraw listbox

DeleteItem function can be used only for 'ownerdraw' items in listbox or combobox. But I need to overide this function or find some other solution to free memory which was allocated for items when they were created.
Is there any way I can do this?
Avatar of Arkadiy
Arkadiy

You may try to override LB_DELETESTRING. When you get it, you know an item is about to be deleted, so this is a good place for cleanup. You may have to trap LB_RESETCONTENT and probably WM_DESTROY as well.
To override, you place the following line in your message map:
ON_MESSAGE(LB_DELETESTRING, OnDeleteString)

and the function is as following:

LRESULT CMyListBox::OnDeleteString(WPARAM wParam, LPARAM lParam)
{
int idx = wParam;
//clean up for idx here
return  CListBox::OnDeleteString(wParam, lParam);
}
Avatar of tflex

ASKER

Seems like this is an idea in the right direction and may be the only one possible, but is there any way to trap WM_DELETEITEM directly without trying to trap all possible cases when items can be deleted? Because in this case it is more easy to make listbox ownerdrawn. The only problem then is DrawItem function. May be anybody has an idea how to implement it without programming all details manually? I need to output just strings.

By the way there is no OnDeleteItem function in CListBox and probably there is no need for it as this is just notification.
There is an OnDeleteItem in CWnd, because the WM_DELETEITEM message goes not to the listbox, but to the listbox's owner. MFC's default handler for this message (in CWnd) reflects it to the listbox, where it's caught by the OnChildNotify method, where it's mapped to the virtual function DeleteItem. You almost always want to override DeleteItem rather than catching WM_DELETEITEM or overriding OnDeleteItem (unless you want to actually do classic owner-draw, and put all of the handling in your CDialog rather than in the CListbox).

Unfortunately, none of this really helps you. You still have a choice between using the solution offered earlier or going with owner draw.

You may want to consider deriving from the MFC class CCheckListbox--which is implemented as an owner-draw listbox control. CCheckListbox will do all of the work for you, and it even hides all of the tricky work it does with the item data pointer (it traps Get and SetItemDataPtr and the related messages so it can let its subclass and/or client pretend that it's a plain listbox) so you can probably use it as-is, as long as you turn off the checkboxes.

The documentation on CCheckListbox is a bit sketchy, but the code (in WinCtrl3.cpp in the MFC 4.2 version) is really well documented and actually lets you in on a few Windows bugs that might have otherwise wasted lots of your time.

If you don't want to use CCheckListbox, at least check out the code; you can probably steal what you need from there.

Oh, and remember to mark the listbox as fixed-height owner-draw in the dialog template.

ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial