How do I create a CCheckListBox in a CFormView

Posted on 1998-07-08
Last Modified: 2013-11-20
Does anyone have some sample code for creating a CCheckListBox in a CFormView?  I have tried to create a new class (CheckList) in Class Wizard and use CCheckListBox as the base class....Guess what.  It wasn't there.
According to Mike Blaszczak in "MFC with Visual C++" I should be able to use the class directly if I conform to some simple rules.  Use LBS_OWNERDRAWFIXED and LBS_HASSTRINGS.  He doen't mention the process however.  Thanks
Question by:graber

Expert Comment

ID: 1318880
Derive a class from CCheckListBox:

class CMyCheckListBox : public CCheckListBox

// Constructors

Then call:
CMyCheckListBox b;
b.Create(LBS_OWNERDRAWFIXED | LBS_HASSTRING, rect, ParentCWnd, ID(can be 100))


Author Comment

ID: 1318881
The usage will be done in a FormView of a split window

Expert Comment

ID: 1318882
1. Using the Dialog Editor, add a ListBox to a dialog, which will be displayed in the CFormView,
2. In the Properties Window adjust properties for a ListBox;- ID to IDC_LIST, Owner draw to "Fixed" and check checkbox "HasString".
3. Define the member variable m_clb in a class derived from the CFormView class

class CCheckLBView : public CFormView

  CCheckListBox m_clb;


4. Override the CFormView function OnInitialUpdate();

void CCheckLBView::OnInitialUpdate()

   VERIFY( m_clb.SubclassDlgItem( IDC_LIST, this) );

  // Adding a string to CheckListBox
  m_clb.AddString( "String 1" );

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


Author Comment

ID: 1318883
I added the verify and checked the ListBox options that you specified and it fails in

BOOL CWnd::Attach(HWND hWndNew)
      ASSERT(m_hWnd == NULL);

This has been the same assertion it has failed on all along,
which is odd because there is MFC code to verify that m_hWnd is not NULL

Expert Comment

ID: 1318884
Is there a line in DoDataExchange() like the following?

DDX_Control( pDX, IDC_LIST, m_clb );

If there is, you should comment it out because this is causing the ASSERT in CWnd::Attach() when you call SubclassDlgItem().

The call to DDX_Control basically attaches the control with id "IDC_LIST" to m_clb.  The ASSERT in CWnd::Attach() makes sure that the control hasn't been attached yet.

Basically, you have two choices:
1. Use SubclassDlgItem and remove the call to DDX_Control in DoDataExchange(), or
2. Use DDX_Control in DoDataExchange and remove the call to SubclassDlgItem() in OnInitialUpdate().

Hope this helps.


Author Comment

ID: 1318885
I do have a data exchange item in the form view
void CViewFormScanCntCntrl::DoDataExchange(CDataExchange* pDX)
        DDX_Control(pDX, IDC_LIST1, m_ChkListBox);
I have removed the subclassing
Thanks mulenga
  We still can't create the window.  Here is the OnInitialUpdate
I use to create the item in question.  The second block showes
where it fails within WinCore.cpp where m_hWnd isn't null

void CViewFormScanCntCntrl::OnInitialUpdate()
      LBS_OWNERDRAWFIXED|LBS_HASSTRINGS,             CRect(5,10,85,80), this, IDC_LIST1);
void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
  _AFX_THREAD_STATE* pThreadState =                                         _afxThreadState.GetData();
  if (pThreadState->m_pWndInit == pWnd)

  if (pThreadState->m_hHookOldCbtFilter == NULL)
      pThreadState->m_hHookOldCbtFilter =              ::SetWindowsHookEx(WH_CBT,
      _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
         if (pThreadState->m_hHookOldCbtFilter == NULL)
  ASSERT(pThreadState->m_hHookOldCbtFilter != NULL);
  ASSERT(pWnd != NULL);
->  ASSERT(pWnd->m_hWnd == NULL);   // only do once
  ASSERT(pThreadState->m_pWndInit == NULL);  
  // hook not already in progress
  pThreadState->m_pWndInit = pWnd;


Expert Comment

ID: 1318886
It is still failing because IDC_LIST1 already has a control identified with it ( the list box in the dialog resource ).

Remove the call to m_ChkListBox.Create() and call
m_ChkListBox.SubclassDlgItem( IDC_LIST1, this ) instead.

Just set the Owner Draw to "Fixed" and check the "Has Strings" in the resource editor.

There are other ways to achieve the same effect but they are not worth the effort but this is the simplest.

Author Comment

ID: 1318887
     I now have a list box with check ooxes ....thank.   I did notice that the check boxes do not change state automatically like a normal check box.  Is that normal?  I am assuming at this point that I need to toggle the state myself when an item is selected.  At any rate thanks....You need to make your next submission an answer.  You really deserve the points.

Accepted Solution

mulenga earned 100 total points
ID: 1318888
I'm not sure about the state of the check box because I have never used them.  Thanks for the points.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
Smart Camera scanning and reading information 3 114
matchUp  challenge 6 70
abstract class with all non abstract mentods 6 80
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 …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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