![]() |
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. |
|
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.
To provide a check box to an application, on the Controls toolbox, click the Check Box button
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.
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:
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.
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. |
|
|