[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

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

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?
0
DickStone
Asked:
DickStone
  • 4
  • 3
1 Solution
 
jkrCommented:
'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.
0
 
DickStoneAuthor 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.
0
 
jkrCommented:
Well, That should be more like

CButton* pCheck = (CButton*)GetDlgItem(nID);

pCheck->SetCheck(BST_CHECKED);

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Thanks
0
 
jkrCommented:
Be sure to also check out http://www.codeproject.com/Articles/537/Avoiding-GetDlgItem ("Avoiding GetDlgItem") - the title says why ;o)
0
 
jkrCommented:
Still having trouble?
0
 
DickStoneAuthor Commented:
Quick response. Also followed up to see if I was doing OK.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now