Check Box Usage – MFC Tutorial 8


Check boxes are used to check binary states. Yes or No answers, Male/Female kind of conditional user interface inputs are easier with a check box. This part of Codersource MFC Tutorial explains how to use a Check box in dialog box applications of MFC.

Check box:

There are quite a few techniques to be learnt with all the MFC Controls. Some of them related to the Check Box control are

  • Handling of Messages ( Mouse Click, Double Click etc)
  • Getting and Setting check box values
  • DDX mechanism

The first job for this MFC tutorial will be to create the MFC Dialog based Application. This application will be used to hold the check boxes. This is the same set of code/steps which are used for the MFC Tutorials explaining about the controls.

To Create the Application:

  • Open the Microsoft Visual Studio 6.00. Click Menu–> File–> New and Select MFC AppWizard(exe) option.
  • In the next MFC AppWizard screen – Step 1, Choose Dialog based application.
  • Click Finish after selecting this. All the files needed for this MFC Tutorial will be created.
  • If the project is built and run, it will open a dialog box, with two command buttons and one static display control.
  • Close the application and come back to the Microsoft Visual studio.

Adding a check box:

The above application and dialog box will be used for handling check box in the tutorial.

The following steps explain how to add a check box for this MFC Tutorial:

  • Open the dialog editor of the previously created application.
  • Click on the check box from the controls toolbox and place two checkboxes on the dialog.
  • Open the properties dialog of the check box. This is done by first selecting the check box and pressing the “Enter” key.
  • Rename the ID of one of the check boxes as IDC_FIRSTCHECKBOX and the caption as “First check box”
  • Rename the second one’s ID as IDC_SECONDCHECKBOX and the caption as Second Check Box.

Now we’ll look at the different ways of manipulating the check boxes.

Event Handling:

Check box can handle the Mouse Click and Double click events. Let us see how to trap the Mouse click events for the check box.

  • Use our previously created application for this MFC Tutorial now. Open the dialog box and Select the “First Check box” control.
  • Press the keys Ctrl + W
  • On the Message Maps tab, select IDC_FIRSTCHECKBOX on the Object ID list box and BN_CLICKED on the
  • Messages list box.
  • Click on Add Function
  • After creating the function, click on the Edit code button. We’ll be taken to the implementation of the click message of the check box.

Add a line for invoking a message box. You can use the following code.

void CMfc_tutorial_8Dlg::OnFirstcheckbox()
{
   // TODO: Add your control notification handler code here
   MessageBox ("First Check box Clicked");
}

Build the application and Run it. If you click on the check box, you should see a message box.

Manipulating the check box:

A Check box can return a value of True or False. Let’s add a Command Button to get the values of check box.

  • Open the dialog editor. Add a Command Button with a caption “Get Second Box”.
  • Name the id with IDC_GETSECONDCHECKBOXVAL.
  • Press Ctrl+W and create a BN_CLICKED handler for it.
  • On the Message Maps tab, Select IDC_GETSECONDCHECKBOXVAL on the Object ID and select the BN_CLICKED Message.
  • Click on Add function with a function name as OnGetSecondBox and add the handler.
  • On the Member Variables tab, select IDC_SECONDCHECKBOX and click on the “Add Variable”.
  • Give the name of the variable as m_ctlSecondCheckBox. On the Category combo, choose Control and the Variable Type will be automatically selected as CButton.

Now open the “mfc_tutorial_7dlg.cpp” (in your case “YourApplicationNameDlg.cpp”) and look for the OnGetSecondBox() function. Add the following code to it.

    void CMfc_tutorial_8Dlg::OnGetSecondBox()
    {
        // TODO: Add your control notification handler code here
		CString l_strCheckBoxVal;
        int l_ChkBox = m_ctlSecondCheckBox.GetCheck();

		l_strCheckBoxVal.Format("Second Check Box Value : %d",l_ChkBox);

		MessageBox(l_strCheckBoxVal);
    }

If the Second Check Box is checked, then the message box will display the string as “Second Check Box Value : 1″. If not, it will display the
string as “Second Check Box Value : 0″. That means, the GetCheck function returns 1 if the control is checked and 0 if it is not.
The variable that we use is m_ctlSecondCheckBox , which was created earlier using the Member variable tab of the class wizard. In the same way we can use SetCheck function to set the values of the check box.

DDX mechanism:

This part of the check box MFC Tutorial deals with an elegant mechanism. This DDX mechanism is an easier way out of the programming intricacies. MFC itself takes care of all the complexities of programming for us. We don’t have to worry about getting or setting the values of check boxes ( or any other controls related), by calling GetCheck or SetCheck.
This is called as Dynamic Data Exchange mechanism. The MFC Framework itself keeps track of the values on the controls. At any point of time, the values are accessible through the
variables declared as the member variables.

Click on the Ctrl+W to open the class wizard In the Member variables tab, select the IDC_SECONDCHECKBOX and click Add Variable Enter the variable name as m_SecondCheckBox Leave the Category and Variable types unchanged at Variable and bool. Click ok. Add a new command Button with a caption “Get DDX” and add a handler to it. We can now get the value of the checkbox without using the GetCheck and SetCheck functions as follows.

void CMfc_tutorial_8Dlg::OnButton1()
{
	// TODO: Add your control notification handler code here
	CString l_strCheckBoxVal;
	UpdateData();
	int l_ChkBox = m_SecondCheckBox;
	UpdateData(FALSE);
	l_strCheckBoxVal.Format("Second Check Box Value : %d",l_ChkBox);
	MessageBox(l_strCheckBoxVal);
}
This is the way MFC handles all the controls from which we need some values to be extracted. This Dynamic Data exchange eases the programmer's job very much. Now we can get the same output as in the manual handling of the check box.

Summary:

This tutorial discussed

  • The usage of check box associated with the class CButton.
  • Handling the Button Clicked messages
  • Usage of functions GetCheck and SetCheck
  • DDX mechanism

Please find the Sample code here.