Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check and uncheck and enable and disable a check Box control in MFC

What is the source code to do the standard checkbox actions with a Visual C++ MFC checkbox control?

  • set a check in the displayed checkbox control
  • clear a check in the displayed checkbox control
  • enable the displayed checkbox control for user input
  • disable the displayed checkbox control for user input
like image 349
Ghebrehiywet Avatar asked May 20 '15 13:05

Ghebrehiywet


2 Answers

Controlling Checkboxes in MFC

Here's how to check, uncheck, enable, and disable a checkbox in MFC:

    CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->SetCheck(0);// uncheck it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->SetCheck(1);// check it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->EnableWindow(0);// disable it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->EnableWindow(1);// enable it
      bool isRemoveChecked = IsDlgButtonChecked(IDC_removeProf);
like image 159
Ghebrehiywet Avatar answered Nov 18 '22 22:11

Ghebrehiywet


Alternatively, you won't need to retrieve a pointer to the button (checkbox) if you use CWnd::CheckDlgButton to check/un-check the button, for example:

BOOL isChecked = ...
CheckDlgButton(IDC_SOME_ID, isChecked);

And, enabling/disabling can be simplified to:

BOOL isEnabled = ...
GetDlgItem(IDC_SOME_ID)->EnableWindow(isEnabled);
like image 33
rrirower Avatar answered Nov 18 '22 22:11

rrirower