Solved

DeleteItem in non-ownerdraw listbox

Posted on 1998-07-24
4
324 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 90
repeatSeparator  java  challenge 13 56
Adapt this command to show who installed 29 106
matchUp  challenge 6 53
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

864 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