• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

How do I create a CCheckListBox in a CFormView

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
1 Solution
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))

graberAuthor Commented:
The usage will be done in a FormView of a split window
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" );

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

graberAuthor Commented:
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
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.

graberAuthor Commented:
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;

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.
graberAuthor Commented:
     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.
I'm not sure about the state of the check box because I have never used them.  Thanks for the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now