How do I create a CCheckListBox in a CFormView

Posted on 1998-07-08
Medium Priority
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
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

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" );

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!


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 300 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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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