Solved

DeleteItem in non-ownerdraw listbox

Posted on 1998-07-24
4
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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