Check Boxes

 

Introduction

A check box is a Windows control that allows the user to set or change the value of an item as true or false. The control appears as a small square o. When this empty square is clicked, it gets marked by a check symbol S. These two states control the check box as checked o or unchecked T.

Like the radio button, the check box doesn't indicate what it is used for. Therefore, it is usually accompanied by a label that displays an explicit and useful string. The label can be positioned on either part of the square box, depending on the programmer who implemented it.

Unlike the radio buttons that implement a mutual-exclusive choice, a check box can appear by itself or in a group. When a check box appears in a group with other similar controls, it assumes a completely independent behavior and it must be implemented as its own entity. This means that clicking a check box has no influence on the other controls.

Check Box Properties

To provide a check box to an application, on the Controls toolbox, click the Check Box button and click the desired area on the dialog box or form. If you require more than one check box, you can keep adding them as you judge necessary.

Like the radio button, a check box is a button with just a different style. Therefore, it shares the same characteristics as the radio button.

Like the radio button, the accompanying label of the check box can appear on the left or the right side of the square box. Also, you can control the horizontal and the vertical alignments of its label using the Horizontal Alignment or the Vertical Alignment properties.

As a radio button can appear as a regular command button with 3-D borders, so can a check box. When a check box with the Push-Like property is up and then clicked, it stays down until it is clicked again, regardless of the appearance of the other controls. Remember that a check box behaves independently of the other controls even if it appears in a group.

A check box can be checked or unchecked. The check box as a control adds a third state for its appearance. Instead of definitely appearing as checked or unchecked, a check box can appear “half-checked”. This is considered as a third state. To apply this behavior to a check box, set its Tri-State property to True or add the BS_3STATE style to it:

A check box that has the Tri-State property set or the BS_3STATE style, can appear checked, unchecked, or dimmed. When using this kind of check box, the user may have to click the control three times in order to get the necessary state.

Check Box Methods

Like the radio button, a check box is based on the CButton class. Therefore, to programmatically create a check box, declare CButton variable or pointer using its constructor. Here is an example:

CButton *HotTempered = new CButton;

After declaring a variable or a pointer to CButton, you can call its Create() method to initialize the control. The syntax is the same as for the radio button. To make the button a check box, its style must include the BS_CHECKBOX value. If you want the control to display or hide a check mark when it gets clicked, create it with the BS_AUTOCHECKBOX style.

BOOL CCheckBoxes::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here
	CButton *chkDay[5];

	chkDay[0] = new CButton;
	chkDay[0]->Create("&Monday", WS_CHILD | WS_VISIBLE | 
	BS_AUTOCHECKBOX | BS_LEFTTEXT,
	CRect(20, 40, 140, 55), this, 0x11);
	chkDay[1] = new CButton;
	chkDay[1]->Create("&Tuesday", WS_CHILD | WS_VISIBLE | 
	BS_AUTOCHECKBOX | BS_LEFTTEXT,
	CRect(20, 60, 140, 75), this, 0x12);
	chkDay[2] = new CButton;
	chkDay[2]->Create("&Wednesday", WS_CHILD | WS_VISIBLE | 
	BS_AUTOCHECKBOX | BS_LEFTTEXT,
	CRect(20, 80, 140, 95), this, 0x13);
	chkDay[3] = new CButton;
	chkDay[3]->Create("&Thursday", WS_CHILD | WS_VISIBLE | 
	BS_AUTOCHECKBOX | BS_LEFTTEXT,
	CRect(20, 100, 140, 115), this, 0x14);
	chkDay[4] = new CButton;
	chkDay[4]->Create("&Friday", WS_CHILD | WS_VISIBLE | 
	BS_AUTOCHECKBOX | BS_LEFTTEXT,
	CRect(20, 120, 140, 135), this, 0x15);

	return TRUE; // return TRUE unless you set the focus to a control
		     // EXCEPTION: OCX Property Pages should return FALSE
}

We mentioned already that a check box can assume one of three states: checked, unchecked, or dimmed. If you want to apply one of the states to such a check box, that is, to programmatically check, uncheck, or dim it, you can call the CWnd::CheckDlgButton() method whose syntax is:

void CheckDlgButton(int nIDButton, UINT nCheck);

The nIDButton argument is the identifier of the check box whose state you want to modify. The nCheck argument is the value to apply. It can have one of the following values:

State Value Description
BST_UNCHECKED The check mark will be completely removed
BST_CHECKED The button will be checked
BST_INDETERMINATE A dimmed checked mark will appear in the control’s square. If the Tri-State property is not set or the BS_3STATE style is not applied to the control, this value will be ignored

Here is an example that automatically sets a check box to a dimmed appearance when the dialog box comes up:

BOOL CCheckBoxes::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here
	CheckDlgButton(IDC_CHECK_PHYSICAL, BST_INDETERMINATE);

	return TRUE; // return TRUE unless you set the focus to a control
		     // EXCEPTION: OCX Property Pages should return FALSE
}

Alternatively, to change the state of a check box, you can call the CButton::SetCheck() and pass of the above state values. For a check box, to set a dimmed check mark, here is an example:

BOOL CCheckBoxes::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// TODO: Add extra initialization here
	CButton *ChkPhysical = new CButton;

	ChkPhysical = reinterpret_cast<CButton *>(GetDlgItem(IDC_CHECK_PHYSICAL));
	ChkPhysical->SetCheck(BST_INDETERMINATE);

	return TRUE; // return TRUE unless you set the focus to a control
		     // EXCEPTION: OCX Property Pages should return FALSE
}

To get the current state of a check box, you can call the CWnd::IsDlgButtonChecked() method. If a check mark appears in the square box, this method will return 1. If the square box is empty, this method returns 0. If the check box control has the BS_3STATE style, the method can return 2 to indicate that the check mark that appears in the square box is dimmed.

Check Box Events

Like all other MFC buttons, the check box can natively send only the click and the double-click messages. These messages respectively are BN_CLICKED and BN_DOUBLECLICKED which originate when the user clicks or double-clicks the control. If you need to, you can use either the events of the parent window or hand code the messages yourself.

Related Articles:

Net Price
Calling one Dialog from Another
Strings in Dialog Box Controls
Dialog Data Transfer
 

Copyright © 2003-2005 FunctionX, Inc.