We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


CCheckListBox error

snoegler asked
Medium Priority
Last Modified: 2013-11-20
(Win95,VC 4.0)
I have a little problem in my actual project:
I have a very simple dialog which uses a dynamically created
CListBox(because of reusing the code), and everything works
fine. Now i tried to change that to a CCheckListBox, because
its features would make everything a bit more pretty.
Take a look at this code snippet:
// selectdialog.h
CSelectDialog : public CDialog()
 CCheckListBox m_cSelListBox;
// selectdialog.cpp
when cListBox' type is CListBox, everything works fine,
but if it is a CCheckListBox i get a gpf, occuring
in CCheckListBox::PreMeasureItem(), and an internal mfc
structure (AFX_CHECK_DATA) contains an invalid pointer.
Does anyone have had an error like this yet?
I am short at time, i hope i'll get soon some hints :o)
Thank you in advance
Watch Question

CheckList boxes have to be Ownerdraw (LBS_OWNERDRAWFIXED) include that in your window style when you create it.

Additionally the doc's say you must derive a class (I'm not sure this is correct as I seem to vaguely remember using it without deriving).  This is a quote :

(begin quote)

CCheckListBox is only for owner-drawn controls because the list contains more than text strings. At its simplest, a checklist box contains text strings and check boxes, but you do not need to have text at all. For example, you could have a list of small bitmaps with a check box next to each item.

To create your own checklist box, you must derive your own class from CCheckListBox. To derive your own class, write a constructor for the derived class, then call Create.

If your checklist box is a default checklist box (a list of strings with the default-sized checkboxes to the left of each), you can use the default CCheckListBox::DrawItem to draw the checklist box. Otherwise, you must override the CListBox::CompareItem function and the CCheckListBox::DrawItem and CCheckListBox::MeasureItem functions.

(end quote)


Sorry this was a failure of mine - i forgot to include the LBS_OWNERDRAWFIXED in the
code i posted ( i had it included in the real code )
 I have localized the error, it is in the lines

void CCheckListBox::PreMeasureItem(...)
  if (measureItem.itemData != 0)
    AFX_CHECK_DATA* pState = (AFX_CHECK_DATA*)measureItem.itemData;
    measureItem.itemData = pState->m_dwUserData; // <-- **error**

Before these lines they give the following comment:
// WINBUG: Windows95 and Windows NT disagree on what this value
// should be.  According to the docs, they are both wrong
(they refer obviously to this itemData member)
I think this is an internal error of Win95 or MFC, because if i set this value (one-time) manually
to zero, it works greatly.
I would give some more points, but i do not have enough at this time.
Yeh I had a look in the sources, good bit hey?

I think setting LBS_HASSTRINGS style would also fix the problem but haven't tried it


Nope, tried that also. Didn't help.
BTW: Thanks for your efforts, if nothing more interesting comes up, i'll give you the points
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


>>"If your checklist box is a default checklist box (a list of strings with the default-sized
checkboxes to the left of each), you can use the default CCheckListBox::DrawItem to drawthe
checklist box. Otherwise, you must override the CListBox::CompareItem function and the
CCheckListBox::DrawItem and CCheckListBox::MeasureItem functions. "

My checklist box should be a 'default' checklist box, so i don't need to override any functions
according to your answer - and that's what i am doing.
It is really easy to test: Create a default 'single document' app using AppWizard,
and insert a CCheckListBox to the CView:
// C<app>View.h
class C<app>View : public CView()
   CCheckListBox m_cCheckListBox;


int CNEditView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;

  // TODO: Add your specialized creation code here


That's all - and it doesn't work. As i said, when tracing, i've set the value measureItem.itemData
to zero and jump over the statement causing the gpf (because the pointer is invalid).
After that it works without any further errors...
I found the solution. Please look at the documentation of the CCheckListBox. It says that you have to over-ride the DrawItem and MeasureItem methods and these are called by the CCheckListBox to find out the size and position of your owner-drawn CheckListBox control. Try doing this.

1. Derive your own CheckListBox from CCheckListBox
2. Add the constructor and the Create methods
3. Add two more member functions viz DrawItem and MeasureItem
4. Implement these methods - constuctor is dummy. create calls CheckListBox create and DrawItem and MeasureItem does nothing.
5. Now derive the CheckListBox in your view class from this CMyCheckListBox instead of the CCheckListBox.
6. Do whatever you want to do with that
7. Compile & Run

You should see this check list box embedded in your view without any assertion.

If you don't do these over-riding (of DrawItem & MeasureItem), you will get an assertion. Hit 'Retry' on the dialog. It should take you to something like

// You must override DrawItem and MeasureItem for LBS_OWNERDRAWVARIABLE

This clearly states that you need to over-ride these methods to get it working.

I have a sample and it works (but with some memory leaks). It doesn't do anything, though. If you are still not getting it right, I can send it across to you.

Hope this helps .....
I have a fully working sample without the memory leaks now and it adds some text entries to the CheckListBox. Sample is available for you to use ....


guruprasad, thanks for all.
But another thing: I've been working with MFC for a not short time - and i've traced myself
into that code. I know the code you mean, and that is *not* the problem. I am getting
a *gpf*, not an assertion - retry will cause the prog to crash, and if i manually set the value ( as i
stated above ) it continues working without problem.
I have found that a CListCtrl does solve the UI requirements in my case better. But anyway,
i'll leave this message for 2-3 days open, and after that i think i'll split the points between you
and answers2000. Thanks, guys.


I'll ask now if they can delete the question, i'll post for each of you a dummy question here.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.