Difference between revisions of "TToggleBox"
m |
|||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{TToggleBox}} | {{TToggleBox}} | ||
− | + | A '''TToggleBox''' [[image:ttogglebox.png]] is a two state labeled button that is enabled or disabled with a single click. It is available on the [[Standard tab]] of the [[Component Palette]]. | |
− | |||
+ | Anywhere in your [[Source code|source code]], you can check the status, whether active or inactive, by query '''<syntaxhighlight lang="pascal" inline>Status := <ToggleBox>.Checked;</syntaxhighlight>'''. You can use ''Checked'' as a normal [[Boolean]]. Thus, even an assignment, '''<syntaxhighlight lang="pascal" inline><ToggleBox>.Checked := True;</syntaxhighlight>''', is possible. | ||
+ | ===A simple example=== | ||
+ | * Create a new [[Application|application]] and drop three TToggleBoxes on the [[TForm|form]]. | ||
+ | * Change the captions of ToggleBox1...3 to ''Red'', ''Green'' and ''Blue'' and it names to ''tbRed'', ''tbGreen'' and ''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'': | ||
+ | <syntaxhighlight lang="pascal" > | ||
+ | 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; | ||
+ | </syntaxhighlight> | ||
+ | * Start your [[Program|program]], it should look something like: | ||
+ | [[image:ToggleBoxExample1.png]] -> [[image:ToggleBoxExample2.png]] | ||
+ | ===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'' and ''tbBlue''. | ||
+ | * Create a ''OnChange'' event handler for one of the ToggleBoxes, e.g. '''<syntaxhighlight lang="pascal" inline>TForm1.tbRedChange(Sender: TObject);</syntaxhighlight>''' 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|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: | ||
+ | <syntaxhighlight lang="pascal" > | ||
+ | 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; | ||
+ | </syntaxhighlight> | ||
− | + | ==See also== | |
+ | * [[doc:lcl/stdctrls/ttogglebox.html|TToggleBox doc]] | ||
+ | * [[TButton]] | ||
+ | * [[TCheckBox]] | ||
+ | * [[TRadioButton]] | ||
− | |||
{{LCL Components}} | {{LCL Components}} | ||
− | |||
− | |||
− |
Latest revision as of 09:10, 9 November 2019
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
A TToggleBox is a two state labeled button that is enabled or disabled with a single click. It is available on the Standard tab of the Component Palette.
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 and 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 and 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