MFC - Checkboxes



A checkbox is a Windows control that allows the user to set or change the value of an item as true or false.

Let us create a new MFC dialog based project.

Once the project is created, you will see the following dialog box in designer window.

Step 1 − Delete the TODO line and drag one checkbox and one Edit control as shown in the following snapshot. Also change the caption of checkbox to Enable Control.

Checkbox

Step 2 − Right-click on the checkbox and select Add Variable.

Checkbox Add Member Var

Step 3 − You can select different options on this dialog box. For checkbox, the CButton variable type is selected by default.

Step 4 − Similarly, the control ID is also selected by default. We now need to select Control in the Category combo box, and type m_enableDisableCheck in the Variable Name edit box and click Finish.

Step 5 − Add Control Variable of Edit control with the settings as shown in the following snapshot.

Checkbox Edit Control

Step 6 − Observe the header file of the dialog class. You can see that these two variables have been added now.

CButton m_enableDisableCheck;
CEdit m_myEditControl;

Step 7 − Right-click on the checkbox and select Add Variable.

Checkbox Add Variable

Step 8 − Click Finish to continue.

Step 9 − Add value Variable for Edit control with the settings as shown in the following snapshot.

Checkbox Edit Variable

Step 10 − Observe the header file. You can see that the new variables have been added now.

bool m_enableDisableVal;
CString m_editControlVal;

Step 11 − Now we will add event handler for checkbox.

Step 12 − Right-click the control for which you want to handle the notification event.

Checkbox Event Handler

Step 13 − Select the event in the Message type box to add to the class selected in the Class list box.

Step 14 − Accept the default name in the Function handler name box, or provide a name of your choice.

Step 15 − Click Add and Edit to add the event handler.

Step 16 − You can now see the following event added at the end of CMFCControlManagementDlg.cpp file.

void CMFCControlManagementDlg::OnBnClickedCheck1() {
   // TODO: Add your control notification handler code here
}

Step 17 − This enables/disables the edit control when the checkbox is checked/unchecked.

Step 18 − We have now added the checkbox click event handler. Here is the implementation of event handler for checkbox.

void CMFCControlManagementDlg::OnBnClickedCheck1() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
}

Step 19 − We need to add the following code to CMFCControlManagementDlg::OnInitDialog(). When the dialog is created, it will manage these controls.

UpdateData(TRUE);
if (m_enableDisableVal)
   m_myEditControl.EnableWindow(TRUE);
else
   m_myEditControl.EnableWindow(FALSE);

Step 20 − Here is the complete implementation of CMFCControlManagementDlg.cpp file.

// MFCControlManagementDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCControlManagement.h"
#include "MFCControlManagementDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx {
   public:
      CAboutDlg();

   // Dialog Data
   #ifdef AFX_DESIGN_TIME
      enum { IDD = IDD_ABOUTBOX };
   #endif

   protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
	
   // Implementation
   protected:
      DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) {

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX) {
   CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
CMFCControlManagementDlg::CMFCControlManagementDlg(CWnd* pParent /* = NULL*/)
   : CDialogEx(IDD_MFCCONTROLMANAGEMENT_DIALOG, pParent), 
   m_enableDisableVal(FALSE), m_editControlVal(_T("")) {
  
   m_hIcon = AfxGetApp()→LoadIcon(IDR_MAINFRAME);
}
void CMFCControlManagementDlg::DoDataExchange(CDataExchange* pDX) {
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_CHECK1, m_enableDisableCheck);
   DDX_Control(pDX, IDC_EDIT1, m_myEditControl);
   DDX_Check(pDX, IDC_CHECK1, m_enableDisableVal);
   DDX_Text(pDX, IDC_EDIT1, m_editControlVal);
}

BEGIN_MESSAGE_MAP(CMFCControlManagementDlg, CDialogEx)
   ON_WM_SYSCOMMAND()
   ON_WM_PAINT()
   ON_WM_QUERYDRAGICON()
   ON_BN_CLICKED(IDC_CHECK1, &CMFCControlManagementDlg::OnBnClickedCheck1)
END_MESSAGE_MAP()


// CMFCControlManagementDlg message handlers

BOOL CMFCControlManagementDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();
	
   // Add "About..." menu item to system menu.
	
   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);
	
   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL) {
      BOOL bNameValid;
      CString strAboutMenu;
      bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
      ASSERT(bNameValid);
      if (!strAboutMenu.IsEmpty()) {
         pSysMenu->AppendMenu(MF_SEPARATOR);
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
   }

   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);      // Set big icon
   SetIcon(m_hIcon, FALSE);     // Set small icon

   // TODO: Add extra initialization here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

void CMFCControlManagementDlg::OnSysCommand(UINT nID, LPARAM lParam) {
   if ((nID & 0xFFF0) == IDM_ABOUTBOX) {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal(); 
   }else {
      CDialogEx::OnSysCommand(nID, lParam);
   }
}
  
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMFCControlManagementDlg::OnPaint() {
   if (IsIconic()) {
      CPaintDC dc(this); // device context for painting

      SendMessage(WM_ICONERASEBKGND,
         reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
			
      // Center icon in client rectangle
      int cxIcon = GetSystemMetrics(SM_CXICON);
      int cyIcon = GetSystemMetrics(SM_CYICON);
      CRect rect;
      GetClientRect(&rect);
      int x = (rect.Width() - cxIcon + 1) / 2;
      int y = (rect.Height() - cyIcon + 1) / 2;

      // Draw the icon
      dc.DrawIcon(x, y, m_hIcon);
   }else{
      CDialogEx::OnPaint();
   }
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCControlManagementDlg::OnQueryDragIcon() {
   return static_cast<HCURSOR>(m_hIcon);
}
void CMFCControlManagementDlg::OnBnClickedCheck1(){
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
}

Step 21 − When the above code is compiled and executed, you will see the following output. You can now see the checkbox is unchecked by default. This disables the edit control.

Edit Control Disable

Step 22 − Now when you check the checkbox, the edit control is enabled.

Edit Control Enabled
mfc_windows_controls.htm
Advertisements