Solved

How do I create a CCheckListBox in a CFormView

Posted on 1998-07-08
9
612 Views
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
GRaber
0
Comment
Question by:graber
9 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1318880
Derive a class from CCheckListBox:

class CMyCheckListBox : public CCheckListBox
{
   DECLARE_DYNAMIC(CMyCheckListBox)

// Constructors
public:
   CMyCheckListBox(){};
 };

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


0
 
LVL 2

Author Comment

by:graber
ID: 1318881
The usage will be done in a FormView of a split window
0
 
LVL 1

Expert Comment

by:timop
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()
{
   CFormView::OnInitialUpdate();

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

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

0
 
LVL 2

Author Comment

by:graber
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:mulenga
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.

0
 
LVL 2

Author Comment

by:graber
ID: 1318885
I do have a data exchange item in the form view
void CViewFormScanCntCntrl::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CViewFormScanCntCntrl)
        DDX_Control(pDX, IDC_LIST1, m_ChkListBox);
    //}}AFX_DATA_MAP
}
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()
{
  CFormView::OnInitialUpdate();
  m_ChkListBox.Create(
      LBS_OWNERDRAWFIXED|LBS_HASSTRINGS,             CRect(5,10,85,80), this, IDC_LIST1);
  m_ChkListBox.AddString("test");
}
void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
{
  _AFX_THREAD_STATE* pThreadState =                                         _afxThreadState.GetData();
  if (pThreadState->m_pWndInit == pWnd)
      return;

  if (pThreadState->m_hHookOldCbtFilter == NULL)
  {
      pThreadState->m_hHookOldCbtFilter =              ::SetWindowsHookEx(WH_CBT,
      _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
         if (pThreadState->m_hHookOldCbtFilter == NULL)
           AfxThrowMemoryException();
  }
  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;
}

0
 

Expert Comment

by:mulenga
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.
0
 
LVL 2

Author Comment

by:graber
ID: 1318887
mulenga
     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.
graber
0
 

Accepted Solution

by:
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
string initialization in java 11 109
sum67 challenge 35 93
My project did see openJDK that I installed. What could be the problem 7 120
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…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now