Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have created a check box in VC++ but I'm not sure how to retrieve the information from it. Do I assign it a variable that is a BOOL OR CButton?

I have tried to assign it the variable BOOL m_Check_Box and I've added the event handler
DDX_Control(pDX, IDC_displaydepthline, m_Check_Box);
but I'm not sure how to retrieve the value in :
void CCleanViewDlg::OnBnClickeddisplaydepthline
{
     UpdateData();
     int l_ChkBox = (int)m_Check_Box;
     UpdateData(FALSE);
}

I get the error:'DDX_Control' : cannot convert parameter 3 from 'BOOL' to 'CWnd &'.

I've also tried making m_Check_Box a CButton and retrieving the value using
int l_ChkBox = m_Check_Box.GetCheck();
but that just sit out a bunch of "Access violation reading location 0x00000000" errors.

Any help would be greatly appreciated!
Thanks!
Posted
Updated 28-Jul-10 2:26am
v3

Well, you misspelled the variable name for one thing. In the event handler, you use m_CheckBox, but you're trying to retrieve the control's staete using m_Check_Box.
 
Share this answer
 
Comments
zeebo17 28-Jul-10 8:24am    
Ah yes sorry about that. I still am getting the same errors though. Can DDX only be used if m_Check_Box is a CButton? If so, is GetCheck() the right way to retrieve the value from a CButton?
You can do it in two ways:


  1. Add a member variable of type CButton, let's say it's named m_btnCheck, then add in your DoDataExchange the following:
    C++
    DDX_Control(pDC, IDC_displaydepthline, m_btnCheck);

    And finally in your handler:
    C++
    void CCleanViewDlg::OnBnClickeddisplaydepthline()
    {
       BOOL bChecked = (m_btnCheck.GetCheck() == BST_CHECKED);
    }

  2. Add a member variable of type int, let's say it's named m_iCheck, then add in your DoDataExchange the following:
    C++
    DDX_Check(pDC, IDC_displaydepthline, m_iCheck);

    And finally in your handler:
    C++
    void CCleanViewDlg::OnBnClickeddisplaydepthline()
    {
       UpdateData();
       BOOL bChecked = (m_iCheck == BST_CHECKED);
    }

 
Share this answer
 
Comments
zeebo17 28-Jul-10 8:56am    
For some reason both of those methods give me "Access violation reading location" errors. When I comment out everything that has to do with the check box it runs smoothly, so the error has to originate from something to do with the check box.

Any suggestions?
zeebo17 29-Jul-10 13:53pm    
Should I be using ON_BN_CLICKED(IDC_displaydepthline, &CCleanViewDlg::OnBnClickeddisplaydepthline);?
Sauro Viti 29-Jul-10 15:11pm    
If you add an handler of this kind, it will be called each time the check-box is clicked, however to retrieve the checked-unchecked status of it you should use one of the methods that I wrote. You can also keep trace of the checked-unchecked status using a BOOL variable and toggling it each time the handler is called, but this is a very bad solution (for instance if you change the check-box status programmatically the handler is not called and you have to treat this case separately).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900