Difference between revisions of "TRadioButton"
(preparation of translation) |
(Added "Usage", "A simple Example", "Usage of a event") |
||
Line 13: | Line 13: | ||
==Usage== | ==Usage== | ||
− | -- in progress --[[User:Michl|Michl]] | + | To use a [[doc:lcl/stdctrls/tradiobutton.html|TRadioButtons]] on a [[TForm/de|Form]], you can simply select it on the component palette ''Standard'' and place it, with one click on the form.<br> |
+ | It usually does not make sense to use a single radiobutton, because radiobuttons are intended to select anything. Thus, you can instead of individual radiobuttons also a [[TRadioGroup]] use.<br> | ||
+ | Anywhere in your source code, you can get the status of the radiobuttons, whether active or inactive, by query '''<code>Status := <RadioButton>.Checked;</code>'''. You can use ''Checked'' as a normal [[Boolean]]. Thus, the allocation '''<code><RadioButton>.Checked := True;</code>''' is possible. | ||
+ | |||
+ | ===A simple Example=== | ||
+ | |||
+ | * Create a new application and drop three TRadioButtons on the form. | ||
+ | * In the Object Inspector tab properties change the name the ''RadioButton1...3'' to ''rbRed'', ''rbGreen'' and ''rbBlue''. | ||
+ | * Similarly, you change the captions of the radiobuttons to ''Red'', ''Green'' and ''Blue'' there. | ||
+ | * Add your form a [[TButton]] and change its caption to ''Draw new'' and its name to ''btnPaint''. | ||
+ | * Create the ''OnClick'' event handler for the TButton, by using the Object Inspector tab events, select the ''OnClick'' event and click the button [...] or double click the button in the form. | ||
+ | * Add following code: | ||
+ | <syntaxhighlight> | ||
+ | procedure TForm1.btnPaintClick(Sender: TObject); | ||
+ | begin | ||
+ | if rbRed.Checked then Color:=clRed; | ||
+ | if rbGreen.Checked then Color:=clLime; | ||
+ | if rbBlue.Checked then Color:=clBlue; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Open your application, it should look something like: | ||
+ | |||
+ | [[image:RadioButtonExample1.png]] -> [[image:RadioButtonExample2.png]] | ||
+ | |||
+ | ===Usage of a event=== | ||
+ | |||
+ | The difference to the previous example is, we repaint the form not by a button click, but already by clicking one of the radio buttons themselves.<br> | ||
+ | |||
+ | You can modify the previous example, by deleting the button and its ''OnClick'' event handler in the source code. You can create a new example but also easy: | ||
+ | * Create a new application and drop three TRadioButtons on the form. | ||
+ | * In the Object Inspector tab properties change the name the ''RadioButton1...3'' to ''rbRed'', ''rbGreen'' and ''rbBlue''. | ||
+ | * Similarly, you change the captions of the radiobuttons to ''Red'', ''Green'' and ''Blue'' there. | ||
+ | * Now you can create the ''OnChange'' event handlers for the radiobuttons. For every radiobutton, you can use the Object Inspector tab events, select the ''Onchange'' event and click the button [...], but you can also doubleclick on it. | ||
+ | * Let the event handler ''OnChange'' of the radio buttons change the colors of the form, according to clicked radio button: | ||
+ | <syntaxhighlight> | ||
+ | procedure TForm1.rbRedChange(Sender: TObject); | ||
+ | begin | ||
+ | Self.Color:=clRed; //with "Self", you select the object in which the method exists (method: rbRedChange / object: Form1) | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.rbGreenChange(Sender: TObject); | ||
+ | begin | ||
+ | Form1.Color:=clLime; //You can directly select the object ''Form1'', but poor, | ||
+ | //because then no other object of class 'TForm1' can be created | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.rbBlueChange(Sender: TObject); | ||
+ | begin | ||
+ | Color:=clBlue; //or you leave out "Self" and the compiler will automatically detect its own object | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Open your application, it should look something like: | ||
+ | |||
+ | [[image:RadioButtonExample3.png]] -> [[image:RadioButtonExample4.png]] | ||
+ | |||
+ | ===Grouping=== | ||
+ | |||
+ | - in progress --[[User:Michl|Michl]] 12:51, 22 May 2014 (CEST) | ||
+ | |||
+ | Fügen Sie Radiobuttons Ihrem Formular hinzu, ist der [[Parent/de|Parent]] (Steuerelement, welches den Radiobutton beinhaltet) Ihr Formular. Bei jedem Setzen (egal, ob per Code oder Nutzer per Buttonklick) eines '''<code><RadioButton>.Checked:=True;</code>''' wird geprüft, ob noch ein anderer Radiobutton, mit diesem Parent, ausgewählt ist und falls ja, dieser auf nicht ausgewählt gesetzt.<br> | ||
+ | Wollen Sie trotzdem mehrere Radiobuttons auf Ihrem Formular nutzen, die verschiedene, unabhängige Auswahlmöglichkeiten bieten sollen, müssen Sie die Radiobuttons gruppieren. Dazu gibt es eine fertige Komponente [[TRadioGroup/de|TRadioGroup]] oder sie gruppieren Sie selber auf einem Control (z.B. [[TPanel/de|TPanel]], [[TGroupBox/de|TGroupBox]], [[TNotebook/de|TNoteBook]], [[TPageControl/de|TPageControl]] etc.).<br> | ||
+ | |||
+ | Folgendes kleines Beispiel zeigt Ihnen, wie sie Radiobuttons selber gruppieren können: | ||
+ | |||
+ | Sie können dazu das Beispiel [[TRadioButton/de#Ein einfaches Beispiel|Ein einfaches Beispiel]] umfunktionieren oder ein neues erstellen: | ||
+ | * Als erstes müssten Sie eine [[TGroupBox/de|TGroupBox]] von der Standard Komponentenpalette auf Ihr Formular platzieren. | ||
+ | * Dieser GroupBox geben Sie den Namen ''gbColor'' und Caption ''Farbe''. | ||
+ | * Nun ordnen Sie dieser Groupbox die Radiobuttons ''rbRed'', ''rbGreen'' und ''rbBlue'' unter: | ||
+ | ** Im veränderten Projekt können Sie nacheinander die Radiobuttons '''im Objektinspektor''' per Drag and Drop auf ''gbColor'' verschieben. | ||
+ | ** In einem neuen Projekt können Sie nacheinander die drei Radiobuttons einfügen, indem Sie zum Einfügen in die Groupbox klicken, die Namen ''rbRed'', ''rbGreen'' und ''rbBlue'' vergeben und die Captions zu ''Rot'', ''Grün'' und ''Blau'' ändern. | ||
+ | * Nun platzieren Sie eine zweite TGroupBox auf Ihrem Formular mit dem Namen ''gbBrightness'' und der Caption ''Helligkeit''. | ||
+ | * Fügen Sie dieser Groupbox ebenfalls drei Radiobuttons hinzu und geben diesen die Namen ''rbBrightDark'', ''rbBrightMedDark'' und ''rbBrightBright'' und die Captions ''Dunkel'', ''Halbdunkel'' und ''Hell''. | ||
+ | * Falls Sie eine neue Anwendung erstellt haben, müssen Sie dem Formular noch einen Button mit dem Namen ''btnPaint'' und Caption ''Neu Zeichnen'' hinzufügen. | ||
+ | * Im ''OnClick''-Eventhandler von ''btnPaint'' ändern Sie den Code zu: | ||
+ | <syntaxhighlight> | ||
+ | procedure TForm1.btnPaintClick(Sender: TObject); | ||
+ | begin | ||
+ | if rbRed.Checked then Color:=Brightness or clRed; | ||
+ | if rbGreen.Checked then Color:=Brightness or clLime; | ||
+ | if rbBlue.Checked then Color:=Brightness or clBlue; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Nun erstellen Sie noch die Function ''Brightness'', indem Sie '''<code>function Brightness: TColor;</code>''' im ''private''-Abschnitt von TForm1 eingeben und [Ctrl]+[Shift]+[c] drücken (Codevervollständigung). Es wird die Function erstellt. Geben Sie dort folgenden Code ein: | ||
+ | <syntaxhighlight> | ||
+ | function TForm1.Brightness: TColor; | ||
+ | begin | ||
+ | Result:=0; | ||
+ | if rbBrightMedDark.Checked then Result:=$888888; | ||
+ | if rbBrightBright.Checked then Result:=$DDDDDD; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Starten Sie Ihr Programm, sie können die gruppierten Radiobuttons voneinader unabhängig einsetzen, so könnte es aussehen: | ||
+ | |||
+ | [[image:RadioButtonExample5.png]] -> [[image:RadioButtonExample6.png]] | ||
+ | |||
+ | ==Siehe auch== | ||
+ | |||
+ | [[TRadioGroup/de|TRadioGroup]] - Verwendung von RadioGroups<br> | ||
+ | [[TToggleBox/de|TToggleBox]] - Verwendung von Toggleboxen<br> | ||
+ | [[TCheckBox/de|TCheckBox]] - Verwendung von CheckBoxen<br> | ||
+ | |||
{{LCL Components Footer |TCheckBox|TListBox}} | {{LCL Components Footer |TCheckBox|TListBox}} | ||
Line 21: | Line 121: | ||
[[Category:LCL]] | [[Category:LCL]] | ||
[[Category:Components]] | [[Category:Components]] | ||
+ | --[[User:Michl|Michl]] 12:51, 22 May 2014 (CEST) |
Revision as of 11:51, 22 May 2014
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
>> LCL Components >> TRadioButton
This page explains how to use the TRadioButton 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 selection button that works with other Radio Buttons in a mutually exclusive way - if one button is selected, none of the others in the group can be selected.
Usage
To use a TRadioButtons on a Form, you can simply select it on the component palette Standard and place it, with one click on the form.
It usually does not make sense to use a single radiobutton, because radiobuttons are intended to select anything. Thus, you can instead of individual radiobuttons also a TRadioGroup use.
Anywhere in your source code, you can get the status of the radiobuttons, whether active or inactive, by query Status := <RadioButton>.Checked;
. You can use Checked as a normal Boolean. Thus, the allocation <RadioButton>.Checked := True;
is possible.
A simple Example
- Create a new application and drop three TRadioButtons on the form.
- In the Object Inspector tab properties change the name the RadioButton1...3 to rbRed, rbGreen and rbBlue.
- Similarly, you change the captions of the radiobuttons to Red, Green and Blue there.
- Add your form a TButton and change its caption to Draw new and its name to btnPaint.
- Create the OnClick event handler for the TButton, by using the Object Inspector tab events, select the OnClick event and click the button [...] or double click the button in the form.
- Add following code:
procedure TForm1.btnPaintClick(Sender: TObject);
begin
if rbRed.Checked then Color:=clRed;
if rbGreen.Checked then Color:=clLime;
if rbBlue.Checked then Color:=clBlue;
end;
- Open your application, it should look something like:
Usage of a event
The difference to the previous example is, we repaint the form not by a button click, but already by clicking one of the radio buttons themselves.
You can modify the previous example, by deleting the button and its OnClick event handler in the source code. You can create a new example but also easy:
- Create a new application and drop three TRadioButtons on the form.
- In the Object Inspector tab properties change the name the RadioButton1...3 to rbRed, rbGreen and rbBlue.
- Similarly, you change the captions of the radiobuttons to Red, Green and Blue there.
- Now you can create the OnChange event handlers for the radiobuttons. For every radiobutton, you can use the Object Inspector tab events, select the Onchange event and click the button [...], but you can also doubleclick on it.
- Let the event handler OnChange of the radio buttons change the colors of the form, according to clicked radio button:
procedure TForm1.rbRedChange(Sender: TObject);
begin
Self.Color:=clRed; //with "Self", you select the object in which the method exists (method: rbRedChange / object: Form1)
end;
procedure TForm1.rbGreenChange(Sender: TObject);
begin
Form1.Color:=clLime; //You can directly select the object ''Form1'', but poor,
//because then no other object of class 'TForm1' can be created
end;
procedure TForm1.rbBlueChange(Sender: TObject);
begin
Color:=clBlue; //or you leave out "Self" and the compiler will automatically detect its own object
end;
- Open your application, it should look something like:
Grouping
- in progress --Michl 12:51, 22 May 2014 (CEST)
Fügen Sie Radiobuttons Ihrem Formular hinzu, ist der Parent (Steuerelement, welches den Radiobutton beinhaltet) Ihr Formular. Bei jedem Setzen (egal, ob per Code oder Nutzer per Buttonklick) eines <RadioButton>.Checked:=True;
wird geprüft, ob noch ein anderer Radiobutton, mit diesem Parent, ausgewählt ist und falls ja, dieser auf nicht ausgewählt gesetzt.
Wollen Sie trotzdem mehrere Radiobuttons auf Ihrem Formular nutzen, die verschiedene, unabhängige Auswahlmöglichkeiten bieten sollen, müssen Sie die Radiobuttons gruppieren. Dazu gibt es eine fertige Komponente TRadioGroup oder sie gruppieren Sie selber auf einem Control (z.B. TPanel, TGroupBox, TNoteBook, TPageControl etc.).
Folgendes kleines Beispiel zeigt Ihnen, wie sie Radiobuttons selber gruppieren können:
Sie können dazu das Beispiel Ein einfaches Beispiel umfunktionieren oder ein neues erstellen:
- Als erstes müssten Sie eine TGroupBox von der Standard Komponentenpalette auf Ihr Formular platzieren.
- Dieser GroupBox geben Sie den Namen gbColor und Caption Farbe.
- Nun ordnen Sie dieser Groupbox die Radiobuttons rbRed, rbGreen und rbBlue unter:
- Im veränderten Projekt können Sie nacheinander die Radiobuttons im Objektinspektor per Drag and Drop auf gbColor verschieben.
- In einem neuen Projekt können Sie nacheinander die drei Radiobuttons einfügen, indem Sie zum Einfügen in die Groupbox klicken, die Namen rbRed, rbGreen und rbBlue vergeben und die Captions zu Rot, Grün und Blau ändern.
- Nun platzieren Sie eine zweite TGroupBox auf Ihrem Formular mit dem Namen gbBrightness und der Caption Helligkeit.
- Fügen Sie dieser Groupbox ebenfalls drei Radiobuttons hinzu und geben diesen die Namen rbBrightDark, rbBrightMedDark und rbBrightBright und die Captions Dunkel, Halbdunkel und Hell.
- Falls Sie eine neue Anwendung erstellt haben, müssen Sie dem Formular noch einen Button mit dem Namen btnPaint und Caption Neu Zeichnen hinzufügen.
- Im OnClick-Eventhandler von btnPaint ändern Sie den Code zu:
procedure TForm1.btnPaintClick(Sender: TObject);
begin
if rbRed.Checked then Color:=Brightness or clRed;
if rbGreen.Checked then Color:=Brightness or clLime;
if rbBlue.Checked then Color:=Brightness or clBlue;
end;
- Nun erstellen Sie noch die Function Brightness, indem Sie
function Brightness: TColor;
im private-Abschnitt von TForm1 eingeben und [Ctrl]+[Shift]+[c] drücken (Codevervollständigung). Es wird die Function erstellt. Geben Sie dort folgenden Code ein:
function TForm1.Brightness: TColor;
begin
Result:=0;
if rbBrightMedDark.Checked then Result:=$888888;
if rbBrightBright.Checked then Result:=$DDDDDD;
end;
- Starten Sie Ihr Programm, sie können die gruppierten Radiobuttons voneinader unabhängig einsetzen, so könnte es aussehen:
Siehe auch
TRadioGroup - Verwendung von RadioGroups
TToggleBox - Verwendung von Toggleboxen
TCheckBox - Verwendung von CheckBoxen
Return To: LCL Components | — Previous: TCheckBox | Next: TListBox |
--Michl 12:51, 22 May 2014 (CEST)