Solved

How do I create a CCheckListBox in a CFormView

Posted on 1998-07-08
9
604 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 99
substring method in java 1 79
matchUp  challenge 6 48
tripleUp challenge 7 67
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 discusses moving either the default database or any database to a new volume.

757 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

20 Experts available now in Live!

Get 1:1 Help Now