• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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?
0
tflex
Asked:
tflex
1 Solution
 
ArkadiyCommented:
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);
}
0
 
tflexAuthor Commented:
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.
0
 
paynCommented:
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.

0
 
Answers2000Commented:
Do you really need to free each item individually as it is destroyed ?  

Unless the list is extremely dynamic, why not simply remember what is added and clean up everything in WM_DESTROY ?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now