Sign in Join
Wiki copytemporary

TRadioButton

Deutsch · English · Français · Suomi · 日本語

A TRadioButton 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

pascal
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;

->

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:

pascal
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;

->

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:

pascal
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;
pascal
function TForm1.Brightness: TColor;
begin
  Result:=0;
  if rbBrightMedDark.Checked then Result:=$888888;
  if rbBrightBright.Checked  then Result:=$DDDDDD;
end;

->

See also


From the Free Pascal wiki, page "TRadioButton", as it stood on 2020-03-01 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.