VCL Controls: The Check Box

 

Introduction

 

Inheritance

A check box is a control that allows the user to make a decision on which item or items in a group would apply to an issue. Although it can appear by itself, a check box usually comes in a group with others, allowing the user to select as many choices as are available. Depending on the compiler or the environment, a little square appears with a check box  S and a label. The user makes his or her selection by clicking in the square which toggles a check mark o. Toggling means if the square were empty o, after clicking it, a check mark would appear in it S; otherwise, the check mark would be removed. Some compilers offer a different kind of check mark: Q or R.

From the user’s standpoint, a check box is selected when its check mark is set; and the item is not selected when its square is empty.

TObject
TPersistent
TComponent
TControl
TWinControl
TButtonControl
TCustomCheckBox

 

From the developer standpoint, a check mark has two (Boolean) values: true or false (or TRUE or FALSE, or True or False). When a check mark is selected, its value is true; otherwise, its value is false. Those are the primary concerns you will have with check marks.A check mark is a special button and can be programmed as a regular button. Therefore, depending on your needs, you can display or hide it at will; you can enable and disable it as necessary; you can adjust its behavior depending on other controls on the same form or the same application.

Check boxes provide what Microsoft calls “non-exclusive” choice, which means each can behave as independent as you want with regard to the other check boxes of the same group. Therefore, although not recommended, you can create check boxes anywhere on the form. It is recommended that you include them in rectangular container so their belonging to the same group will be obvious to the user. The group can be hosted by a GroupdBox (recommended) or a Panel controls. You can also design the group in a Bevel control but, since the bevel is not a container, you will not be able to perform some of moving and toggling operations you get with the first suggested controls.

Check Box Properties

If you are planning to have just one check box, from the Standard tab of the Component Palette, click the CheckBox control and click on the desired section of the form or container. If you are planning to use more than one check box, you should first position a group control to your form, then add the desired CheckBox controls.

The most obvious property of the check box is its state as being checked or not. By default, a check box is not checked (it is empty). At design time, you can make sure that a check box appears checked or not by changing the Boolean value of the Checked property in the Object Inspector. When you set this property, it would be updated automatically on the form. You or the user can also control this property at runtime. To change the Checked property programmatically, you could write:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    CheckBox1->Checked = True;
}
//---------------------------------------------------------------------------

When a check box is clicked, its Checked property has a value of true; otherwise, the values is false. Since the Checked property is a Boolean value, you can toggle its state based on an intermediary action from the program, the user, or the computer:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    CheckBox1->Checked = !CheckBox1->Checked;
}
//---------------------------------------------------------------------------

Probably the most important property of a check box is its caption, which is controlled by the Caption property. This can easily be set at design or runtime. The position of the caption is controlled by the Alignment property. By default, the check box caption is aligned to the right side of the check box:


 
Right-aligned caption

 
Left-aligned caption

To change the caption alignment of a check box, use the Alignment property of the Object Inspector. The values are taRightJustify for the right alignment and the taLeftJustify.

Fundamentally, a check box can have only one of two states: checked or unchecked. When a check box’ checked property is dependent of other controls or actions, sometimes, you cannot categorically set it to Checked or not Checked. Imagine that, in a certain company, for an employee to qualify for stock options, he or she must have a full-time status and must have been in the company for at least two years. If an (important) employee fulfills one of these requirements bu not the other requirements, you can gray out the Stock Options check box. since the control would not be completely checked, you can show it as half checked:

This property is set using the AllowGrayed property. In the Object Inspector, change the (Boolean) of the desired check box to true or false (the default). Programmatically, to set this property, assign it a true value (because otherwise the false value is set by default).

At design time or when the user is interacting with your application, you can control the display of a check box using one of three states. Unlike being checked or unchecked, like the AllowGrayed property, you can set an intermediary that would help you and user know that the control’s condition cannot be definitely decided. This is set using the State property. This property, derived from the TCheckBoxState enumerator, allows you to set a check box as unchecked (the default) by assigning the cbUnchecked value; to definitely check it, set this property to cbChecked. As a 3rd alternative, you can assign it a cbGrayed value.

Check Box Methods and Events

The check box control has only its constructor and its destructor as methods. The constructor allows you to dynamically create the control. To do this, use the new operator to assign a TCheckBox object to the named instance of the control. You must also specify what control, namely the form owns the check box because the compiler wants to know “who” would clean after the object when the control is not used anymore:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TCheckBox* Checker = new TCheckBox(Form1);
    Checker->Parent = Form1;
}
//---------------------------------------------------------------------------

If the check box will be hosted by a container other than the form, specify this as the parent:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TCheckBox* Checker = new TCheckBox(Form1);
    Checker->Parent = GroupBox1;
}
//---------------------------------------------------------------------------

If you do not have or cannot get a container at design time, you can also dynamically create one that would host the dynamic check boxes. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TGroupBox* Group = new TGroupBox(Form1);
    Group->Parent = Form1;
    Group->Left = 16;
    Group->Top = 32;
    Group->Caption = "Preferred Sports";

    TCheckBox* chkFootball = new TCheckBox(Form1);
    chkFootball->Parent = Group;
    chkFootball->Left = 16;
    chkFootball->Top = 16;
    chkFootball->Alignment = taRightJustify;
    chkFootball->Caption = "Football";
    chkFootball->Checked = True;

    TCheckBox* chkHandball = new TCheckBox(Form1);
    chkHandball->Parent = Group;
    chkHandball->Left = 16;
    chkHandball->Top = 36;
    chkHandball->Caption = "Handball";
}
//---------------------------------------------------------------------------

To create this control globally, that is, to make it accessible by more than one function, method or event, declare it in the private or public sections of the header file of the unit or object (control) that would “own” it. We have seen various examples of doing this already.

 

 
 

Home Copyright © 2004-2007 FunctionX, Inc.