We help IT Professionals succeed at work.

How do I get/set information from a checkbox control in MFC

960 Views
Last Modified: 2014-08-26
If I have a text control, I can get/set information as:

CString strVal;
GetDlgItem(nID)->      GetWindowText(strVal);
GetDlgItem(nID)->      GetWindowText(strVal);

What is the equivalent to get/set information from a checkbox control?
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
'CButton::GetCheck()' (http://msdn.microsoft.com/en-US/library/8tba1y6f%28v=vs.110%29.aspx) or 'CButton::SetCheck()' (http://msdn.microsoft.com/en-us/library/31deb72x(v=vs.110).aspx) will do that for your. See also the article at http://www.codeproject.com/Articles/537/Avoiding-GetDlgItem ("Avoiding GetDlgItem") that sheds in some light about how MFC handles that.

Author

Commented:
I tried

GetDlgItem(nID)->CButton::SetCheck(BST_CHECKED);

This resulted in:
c:\wfree\runpage.cpp(1264): error C2039: 'CButton' : is not a member of 'CWnd'
2>          c:\program files\microsoft visual studio 10.0\vc\atlmfc\include\afxwin.h(2286) : see declaration of 'CWnd'
2>c:\wfree\runpage.cpp(1264): error C2662: 'CButton::SetCheck' : cannot convert 'this' pointer from 'CWnd' to 'CButton &'
2>          Reason: cannot convert from 'CWnd' to 'CButton'
2>          Conversion requires a second user-defined-conversion operator or constructor    


P.S. I normally use variables, but there are 45 controls and it's easier to set up a loop on the controls.
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That looks like it got it. I'll close this tomorrow if I don't get any additional comments.

Thanks
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Be sure to also check out http://www.codeproject.com/Articles/537/Avoiding-GetDlgItem ("Avoiding GetDlgItem") - the title says why ;o)
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Still having trouble?

Author

Commented:
Quick response. Also followed up to see if I was doing OK.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.