Difference between revisions of "TRadioButton"

From Lazarus wiki
Jump to navigationJump to search
(Added "Usage", "A simple Example", "Usage of a event")
m (Fixed syntax highlighting)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{TRadioButton}}
 
{{TRadioButton}}
  
>> [[LCL Components]] >> TRadioButton<br/>
+
A '''TRadioButton''' [[image:tradiobutton.png]] is a component that displays 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. TRadioButtons are selectable from the [[Standard tab]] of the [[Component Palette]].
  
This page explains how to use the [[doc:lcl/stdctrls/tradiobutton.html|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.
+
To use a TRadioButton on a [[TForm|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.
  
==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.
 
 
[[image:Comp_Standard_TRadioButton.png]]
 
 
==Usage==
 
 
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.
 
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===
+
===A simple example===
  
 
* Create a new application and drop three TRadioButtons on the form.
 
* Create a new application and drop three TRadioButtons on the form.
Line 25: Line 16:
 
* 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.
 
* 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:
 
* Add following code:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.btnPaintClick(Sender: TObject);
 
procedure TForm1.btnPaintClick(Sender: TObject);
 
begin
 
begin
Line 39: Line 31:
 
===Usage of a event===
 
===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>
+
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:
 
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:
Line 47: Line 39:
 
* 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.
 
* 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:
 
* Let the event handler ''OnChange'' of the radio buttons change the colors of the form, according to clicked radio button:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.rbRedChange(Sender: TObject);
 
procedure TForm1.rbRedChange(Sender: TObject);
 
begin
 
begin
Line 64: Line 57:
 
end;  
 
end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
* Open your application, it should look something like:
 
* Open your application, it should look something like:
  
Line 70: Line 64:
 
===Grouping===
 
===Grouping===
  
- in progress --[[User:Michl|Michl]] 12:51, 22 May 2014 (CEST)
+
If you add a radiobutton to your form is its [[Parent|parent]] (control, which includes the radio button) your form. By each setting (no matter whether via code or user button click) of a '''<code><RadioButton>.Checked:=True;</code>''' is checked whether a different radiobutton, with this parent, is selected and if yes, the property ''Checked'' of this would be changed to ''False''.<br>
 +
If you want to use multiple radiobuttons on your form, that are designed to provide different, independent choices, you must group the radio buttons. There are a finished component [[TRadioGroup]] or you group on a control (e.g. [[TPanel]], [[TGroupBox]], [[TNotebook]], [[TPageControl]] etc.).<br>
  
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>
+
The following example shows how you can group radio buttons:
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:
+
You can change the example [[TRadioButton#A simple example|A simple example]] or create a new application:
 +
* As first you would need to place a [[TGroupBox]] of the standard component palette onto your form.
 +
* You change its name to ''gbColor'' and its caption to ''Color''.
 +
* Now you subclass this GroupBox that radio buttons ''rbRed'', ''rbGreen'' and ''rbBlue'':
 +
** In the modified project, you can sequentially move the radiobuttons '''in the Object Inspector''' by drag and drop to ''gbColor''.
 +
** In a new project, you can insert the three radiobuttons one after the other, by clicking to insert in the GroupBox, then change the names to ''rbRed'', ''rbGreen'' and ''rbBlue'' and the captions to ''Red'', ''Green'' and ''Blue''.
 +
* Now place a second TGroupBox on your form named ''gbBrightness'' with the caption ''Brightness''.
 +
* Add this GroupBox also three radio buttons and give it the name ''rbBrightDark'', ''rbBrightMedDark'' and ''rbBrightBright'' and the captions ''Dark'', ''MediumDark'' and ''Bright''.
 +
* If you have created a new application, you must add a button with name ''btnPaint'' and caption ''Draw new'' to the form.
 +
* In the ''OnClick'' event handler of ''btnPaint'' change the code to:
  
Sie können dazu das Beispiel [[TRadioButton/de#Ein einfaches Beispiel|Ein einfaches Beispiel]] umfunktionieren oder ein neues erstellen:
+
<syntaxhighlight lang=pascal>
* 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);
 
procedure TForm1.btnPaintClick(Sender: TObject);
 
begin
 
begin
Line 95: Line 88:
 
end;
 
end;
 
</syntaxhighlight>
 
</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>
+
* Now create even the function Brightness, by enter the ''private'' section of TForm1, write '''<code>function Brightness: TColor;</code>''' and press the keys [CTRL] + [Shift] + [c] (code completion). The function is created. Enter there following code:
 +
 
 +
<syntaxhighlight lang=pascal>
 
function TForm1.Brightness: TColor;
 
function TForm1.Brightness: TColor;
 
begin
 
begin
Line 104: Line 99:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
* Starten Sie Ihr Programm, sie können die gruppierten Radiobuttons voneinader unabhängig einsetzen, so könnte es aussehen:
+
 
 +
* Start your application, you can use the grouped radio buttons separate, so it could look like:
  
 
[[image:RadioButtonExample5.png]] -> [[image:RadioButtonExample6.png]]
 
[[image:RadioButtonExample5.png]] -> [[image:RadioButtonExample6.png]]
  
==Siehe auch==
+
==See also==
 
 
[[TRadioGroup/de|TRadioGroup]] - Verwendung von RadioGroups<br>
 
[[TToggleBox/de|TToggleBox]] - Verwendung von Toggleboxen<br>
 
[[TCheckBox/de|TCheckBox]] - Verwendung von CheckBoxen<br>
 
  
 +
* [[doc:lcl/stdctrls/tradiobutton.html|TRadioButton doc]]
 +
* [[TRadioGroup]] - Usage of RadioGroups<br>
 +
* [[TToggleBox]] - Usage of Toggleboxes<br>
 +
* [[TCheckBox]] - Usage of CheckBoxes<br>
  
{{LCL Components Footer |TCheckBox|TListBox}}
 
 
{{LCL Components}}
 
{{LCL Components}}
 
 
[[Category:LCL]]
 
[[Category:Components]]
 
--[[User:Michl|Michl]] 12:51, 22 May 2014 (CEST)
 

Latest revision as of 12:18, 1 March 2020

Deutsch (de) English (en) suomi (fi) français (fr)

A TRadioButton tradiobutton.png is a component that displays 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. TRadioButtons are selectable from the Standard tab of the Component Palette.

To use a TRadioButton 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:

RadioButtonExample1.png -> 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.

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:

RadioButtonExample3.png -> RadioButtonExample4.png

Grouping

If you add a radiobutton to your form is its parent (control, which includes the radio button) your form. By each setting (no matter whether via code or user button click) of a <RadioButton>.Checked:=True; is checked whether a different radiobutton, with this parent, is selected and if yes, the property Checked of this would be changed to False.
If you want to use multiple radiobuttons on your form, that are designed to provide different, independent choices, you must group the radio buttons. There are a finished component TRadioGroup or you group on a control (e.g. TPanel, TGroupBox, TNotebook, TPageControl etc.).

The following example shows how you can group radio buttons:

You can change the example A simple example or create a new application:

  • As first you would need to place a TGroupBox of the standard component palette onto your form.
  • You change its name to gbColor and its caption to Color.
  • Now you subclass this GroupBox that radio buttons rbRed, rbGreen and rbBlue:
    • In the modified project, you can sequentially move the radiobuttons in the Object Inspector by drag and drop to gbColor.
    • In a new project, you can insert the three radiobuttons one after the other, by clicking to insert in the GroupBox, then change the names to rbRed, rbGreen and rbBlue and the captions to Red, Green and Blue.
  • Now place a second TGroupBox on your form named gbBrightness with the caption Brightness.
  • Add this GroupBox also three radio buttons and give it the name rbBrightDark, rbBrightMedDark and rbBrightBright and the captions Dark, MediumDark and Bright.
  • If you have created a new application, you must add a button with name btnPaint and caption Draw new to the form.
  • In the OnClick event handler of btnPaint change the code to:
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;
  • Now create even the function Brightness, by enter the private section of TForm1, write function Brightness: TColor; and press the keys [CTRL] + [Shift] + [c] (code completion). The function is created. Enter there following code:
function TForm1.Brightness: TColor;
begin
  Result:=0;
  if rbBrightMedDark.Checked then Result:=$888888;
  if rbBrightBright.Checked  then Result:=$DDDDDD;
end;
  • Start your application, you can use the grouped radio buttons separate, so it could look like:

RadioButtonExample5.png -> RadioButtonExample6.png

See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu