Win32 Controls: Check Boxes |
|
Introduction to Check Boxes |
Description |
A check box is a Windows control that allows the user to set or change the value of an item as true or false. Although it can appear by itself, a check box sometimes comes in a group with others, allowing the user to select as many choices as are available. Depending on the application, a little square appears. |
The user makes a selection by clicking in the square which toggles a check mark . Toggling means that if the square were empty, after clicking it, a check mark would appear in it. Otherwise, the check mark would be removed. Like a radio button, a check box is usually accompanied by a label to indicate what the check control is used for. 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. 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. |
To support check boxes, the VCL provides the a TCheckBox class. The TCheckBox class is derived from the TCustomCheckBox class. Both classes are members of the StdCtrls.hpp header file. In Microsoft Windows, a check box is just a special button. For this reason, the TCustomCheckBox class is based on the TButtonControl class. The TButtonControl class is based on the TWinControl class:
To visually create a check box, from the Standard section of the Tool Palette, click the TCheckBox control and click the desired section of the form or container. In the same way, you can add as many check boxes as you want. If you want to use more than one check box, you should first place a group control on your form, then add the desired check boxes in it. To dynamically create the control, declare a variable of type TCheckBox, use the new operator to initialize the object. You must also specify what control owns the check box. Here is an example: //--------------------------------------------------------------------------- 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 = Group;
}
//---------------------------------------------------------------------------
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. Check boxes provide "non-exclusive" choice, which means that each check box behave independently with regards to the other check boxes of the same container. If you are creating just one check box, you can place it where you want on the form. If you are creating more than one check box that are addressing the same issue, you should include them in a rectangular container so their belonging to the same group would be obvious to the user. The group can be hosted by a group box, a bevel, a radio group, or a panel controls. If you place the controls in a bevel or a radio group, since these two are true containers for other controls, you will not be able to move all controls as one object during design. 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; TCheckBox* chkHandball = new TCheckBox(Form1); chkHandball->Parent = Group; } //---------------------------------------------------------------------------
|
A check box is programmed as a regular control. It inherits the location from its ancestors: the Left and Top properties. You can specify these characteristics during design or at run time. 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; TCheckBox* chkHandball = new TCheckBox(Form1); chkHandball->Parent = Group; chkHandball->Left = 16; chkHandball->Top = 36; } //--------------------------------------------------------------------------- An important characteristic of a check box is its title, which is controlled by the Caption property that it inherits from the TControl class. This can easily be set at design or runtime. 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->Caption = "Football"; TCheckBox* chkHandball = new TCheckBox(Form1); chkHandball->Parent = Group; chkHandball->Left = 16; chkHandball->Top = 36; chkHandball->Caption = "Handball"; } //--------------------------------------------------------------------------- Depending on the application’s needs, you can display or hide it (using the Visible property), enable or disable it (using the Enabled property). You can adjust the control’s behavior depending on other controls on the same form, the same application, or external factors. Most of the other characteristics a check box uses derive from its ancestors the TControl and the TWinControl classes.
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). To support this, the check box inherits the Checked property from the TButtonControl class: __property bool Checked = {read=GetChecked,write=SetChecked}; 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, simply assign a True or False value to the Checked property. Here is an example: //--------------------------------------------------------------------------- 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 value 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; } //--------------------------------------------------------------------------- Algebra & Trigonometry - Page 447: Solving a right triangle: We know 2 sides and an angle of a triangle (3 measures). Use 3 check boxes to let the user specify what 2 values are known (the user must select only check boxes, not all three. Add a group of two radio buttons. The user must select the radio button of the angle that is known. Find the other two sides and the other angle.
The position of the caption is controlled by the Alignment property. This property is of type TAlignment: __property Classes::TAlignment Alignment = {read=FAlignment,write=SetAlignment}; By default, the control's caption is aligned to the right side of the check box. To change the caption alignment of a check box, use the Alignment property of the Object Inspector:
The TAlignment enumerator has two members: taRightJustify for the right alignment and the taLeftJustify. You can specify the alignment with code. 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;
}
//---------------------------------------------------------------------------
If you want to know the alignment applied on a check box, call the GetControlsAlignment() method of the TControl class. Its syntax is: TAlignment __fastcall GetControlsAlignment(void);
Fundamentally, a check box can have 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, she must be 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 but 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 controlled by the AllowGrayed property: __property bool AllowGrayed = {read=FAllowGrayed,write=FAllowGrayed}; In the Object Inspector, change the (Boolean) AllowGrayed property 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 use an intermediary state that would help you and/or the user know that the control’s condition cannot be definitely decided. This is set using the State property. This property, based on the TCheckBoxState enumerator: __property Stdctrls::TCheckBoxState State = {read=FState,write=SetState}; The TCheckBoxState enumerator is defined as follows: enum TCheckBoxState{ cbUnchecked, cbChecked, cbGrayed }; The State property 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.
As far as Microsoft Windows is concerned, a check box is just a modified button. This allows it to use the common characteristics of command buttons and radio controls. Based on this, when the user clicks a check box, an OnClick() event fires. All the other events derive from its ancestors the TControl and the TWinControl classes.
|
|
|||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|