TToggleBox
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
>> LCL Components >> TToggleBox
This page explains how to use the TToggleBox component. When I mention to click on something, unless I explicitly say to right-click, you always left-click on the item in question.
Description
A labelled box capable of being checked or unchecked. It is similar to a TButton, but it is enabled or disabled with a single click.
Usage
To use a TToggleBox on a form, you can simply select it on the Standard component pallet and place it by clicking on the form.
Anywhere in your source code, you can check the status, whether active or inactive, by query Status := <ToggleBox>.Checked;
. You can use Checked as a normal Boolean. Thus, even an assignment, <ToggleBox>.Checked := True;
, is possible.
A simple example
- Create a new application and drop three TToggleBoxes on the form.
- Change the captions of ToggleBox1...3 to Red, Green and Blue and it names to tbRed, tbGreen und tbBlue.
- Add to your form a TButton and change its caption to Paint new and its name to btnPaint.
- Create the OnClick event handler for the TButton: go in the Object Inspector tab events, select the OnClick event and the [...] button or simple doubleclick it on the form.
- Add following code in the event handler of btnPaint:
procedure TForm1.btnPaintClick(Sender: TObject);
var
aColor: TColor;
begin
aColor:=0; //Background color of Form1 is set according to the Toggleboxes
if tbRed.Checked then aColor:=aColor + $0000FF;
if tbGreen.Checked then aColor:=aColor + $00FF00;
if tbBlue.Checked then aColor:=aColor + $FF0000;
Color := aColor; //the change of the property <Formular>.Color causes a redrawing of the form
end;
- Start your program, it should look something like:
Use an event
The difference to the previous example is, the form would not be repainted by a button click, but already by clicking on one of the toggleboxes itself.
You can modify the previous example, by deleting the button and its OnClick event handler in the source code. But also easy, you can create a new example:
- Create a new application and drop three TToggleBoxes on the form.
- Change the captions of ToggleBox1...3 to Red, Green and Blue and it names to tbRed, tbGreen und tbBlue.
- Create a OnChange event handler for one of the ToggleBoxes, e.g.
TForm1.tbRedChange(Sender: TObject);
and also connect the other ToggleBoxes with it:- Doubleclick tbRed on your form or select tbRed on your form and go in the Object Inspector on the tab events, select the OnChange event and click on the button [...].
- It creates the procedure tbRedChange.
- Now select tbGreen on your form.
- Go in the Object Inspector to the tab events, choose the OnChange event and select tbRedChange in the adjacent combobox.
- Now on your form, select tbBlue and proceed as with tbGreen.
- Get the event handler OnChange of the ToggleBoxes the colors of the form, according to <ToggleBox>.Checked, change:
procedure TForm1.tbRedChange(Sender: TObject);
var
aColor: TColor;
begin
aColor:=0; //Background color of Form1 is set according to the Toggleboxes
if ToggleBox1.Checked then aColor:=aColor + $0000FF;
if ToggleBox2.Checked then aColor:=aColor + $00FF00;
if ToggleBox3.Checked then aColor:=aColor + $FF0000;
Color := aColor; //the change of the property <Formular>.Color causes a redrawing of the form
end;
See also
TButton - Usage of Buttons
TCheckBox - Usage of CheckBoxes
TRadioButton - Usage of RadioButtons
Return To: LCL Components | — Previous: TMemo | Next: TCheckBox |
--Michl 13:46, 20 May 2014 (CEST)