How to Use checkboxes in MFC

A checkbox control has two states, "checked" and "unchecked." It can be applied wherever a choice is expected of the user, such as agreeing to the conditions set by a vendor before installing a software or selecting multiple choice answers. The checkbox is based on the CButton class of the MFC (Microsoft Foundation Class) Library. Using MFC Check Box controls in a program is fairly simple to learn.

Things You'll Need

  • Microsoft Visual Studio
  • Basic MFC
Show More

Instructions

    • 1

      Launch Microsoft Visual Studio. Use the MFC AppWizard (exe) to create a Dialog-based project. Choose the default options.

    • 2

      Place a Check Box on the Dialog Control you just created from the controls toolbox. Right-click it, select "Properties" and give it the ID "IDC_CHECKBOX."

    • 3

      Create an event function for the Check Box. Open the Class Wizard by pressing Ctrl+W, then click the "Message Maps" tab. Select "IDC_CHECKBOX1" under "Object IDs." Next, under "Messages," select "BN_CLICKED." Now click the "Add Function" button. Accept the default function name and click "OK." Click "Edit Code" in the wizard window. Paste the following line under the "TODO" comment: "MessageBox("Clicked!");". Compile and run the code. Check the checkbox and see the message.

    • 4

      Demonstrate the binary function of the checkbox control. Open the Class Wizard by hitting Ctrl+W. Click the "Member Variables" tab and highlight "IDC_CHECKBOX1." Click the "Add Variable" button and name the variable "m_MyCheckBox." Select "Control" under the "Category" combo. Go back to the Design view and add a Button on the Dialog control. Right-click the Button, select "Properties" type for ID "IDC_BINARYBUTTON" and change the caption to "Get Val." Press Ctrl+W, click the "Message Maps" tab, choose "IDC_BINARYBUTTON" under "Object ID" and highlight the "BN_CLICKED" message. Click "Add Function," accept the default name and click "Edit Code." Paste the following code under the "TODO" comment:



      CString strBinaryVal;

      int val = m_MyCheckBox.GetCheck();

      strBinaryVal.Format("The Check Box Value is : %d",strBinaryVal);

      MessageBox(strBinaryVal);
    • 5

      Compile and run the code again. When the button is pressed and the checkbox is set, the number 1 will appear. A zero will appear when the checkbox control isn't checked.

Related Searches:

Resources

Comments

  • AlexDen Nov 03, 2008
    The code example is very illustrative. It helped me to quickly use a check box when I didn't have time to read a manual.
  • AlexDen Nov 03, 2008
    The code example is very illustrative. It helped me to quickly use a check box when I didn't have time to read a manual.

You May Also Like

  • MFC ActiveX Tutorial

    The ActiveX Control program is used by programmers to configure how different applications communicate and share information. ActiveX can be used with...

  • How to Use a Listbox Control in MFC Visual C++

    A list box is a Windows control that can display text or iconic items as a list of rows. The Microsoft Foundation...

  • How to Use MFC Toolbars

    A toolbar is a control bar that holds small bitmap image buttons of controls and commands which are used in Microsoft Foundation...

  • How to Make MFC Dialog Boxes

    A dialog box is a basic window derived from the CDialog class of the Microsoft Foundation Class (MFC) Library. You can use...

  • How to Use Edit Controls in MFC

    In Microsoft Windows based systems the CEdit class provides the functions of the edit controls in MFC. In a dialog based program...

  • How to Manipulate List View Controls in MFC

    List view controls are the controls that are used to show data in different formats. These formats are icon view, report view...

  • How to Create Message Maps in MFC

    A Windows operating system is message driven environment. An event like a mouse click or a printer signal sends a message to...

  • ActiveX Component Tutorial

    An ActiveX component is an applet that can be downloaded and run independently in Microsoft's Web browser. It can also be "plugged...

  • How to Add a CheckBox to a DataGrid

    A DataGrid control in Microsoft Visual Basic allows programmers to display data in a grid format. The DataGrid contains rows and columns...

  • How to Use a Cell as a Checkbox in Excel 2007

    If you want to create an electronic form used to check specific options, you can easily added checkboxes to the spreadsheet of...

Related Ads

Featured