Solved

DeleteItem in non-ownerdraw listbox

Posted on 1998-07-24
4
313 Views
Last Modified: 2013-11-20
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
Comment
Question by:tflex
4 Comments
 
LVL 1

Expert Comment

by:Arkadiy
ID: 1319701
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
 

Author Comment

by:tflex
ID: 1319702
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
 
LVL 1

Expert Comment

by:payn
ID: 1319703
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
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1319704
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now