TRadioGroup
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
TRadioGroup is a group of related but mutually exclusive TRadioButtons, requiring the user to select one of a set of alternatives. It's like a TGroupBox with integrated TRadioButtons. To use a TRadioGroup on a form, you can simply select it on the Standard tab of the Component Palette and place it by clicking on the form.
TRadioGroup manages its items as a TStrings list in the Items property. The Object Inspector provides a small text input where each line represents one entry in the radio group box.
With the Columns property the layout can be easily adapted to a column-layout. The property ColumnLayout allows choosing the insertion direction "horizontal then vertical" or vice versa.
The currently selected item is not determined via a checked property as the TRadioButton provided, instead an ItemIndex is used here. -1 is equal to "no selection", 0 selects the first option, 1 the second and so forth. ItemIndex is both readable and writable.
The caption of the group can be changed by using the Caption property. Changes on the Font property influence the caption as well as the text of the items inside the box.
Code Examples:
// Configuration:
with RadioGroup1 do
begin
caption := 'My Group 1';
itemIndex := 0;
font.size := 12;
end;
// Conditional Check:
if RadioGroup1.ItemIndex = 0 then
begin
...
end;
// Conditional Check with Case:
case RadioGroup1.ItemIndex of
0: begin
firstOption;
...
end;
1: begin
secondOption;
...
end;
2: thirdOption;
3: fourthOption;
...
end;
Examples
Drawing random shapes on a form
Geometric figures should be drawn randomly and depending on the TRadioGroups to the form. The first RadioGroup determines the shape, the second RadioGroup detrmines the number.
- create a new application and place two TRadioGroups on your form
- change in the Object Inspector the property Name of RadioGroup1 to rgShape, also RadioGroup2 to rgCount
- change identical Caption of rgShape to Shape and that of rgCount to Count
- add the RadioButtons for rgShape:
- in the Object Inspector select the property Items of rgShape
- click on the button [...], the character chain editor opens
- write among each other Lines Rectangles Ellipses and complete the entry with the button OK
- add identical the RadioButtons for rgCount (written among each other): 1 5 10 20 50 100
- set the first RadioButton as the currently selected, by setting the property ItemIndex of rgShape and rgCount from -1 to 0
- create the OnClick event handler of rgShape by double clicking rgShape
- also use these event handler for rgCount:
- in the Object Inspector select rgCount
- now select the tab Events in the Object Inspector
- go to the OnClick event and select in the adjacent ComboBox rgShapeClick
- whenever rgShape or rgCount is clicked, the form should be redrawn, therefore write following code in the event handler:
procedure TForm1.rgShapeClick(Sender: TObject);
begin
Repaint;
end;
- whenever the form is redrawn, the shapes should be drawn:
- in the Object Inspector select Form1
- select the tab Events
- click on the button [...] next to the event OnPaint
- the handler is created, enter the following code:
procedure TForm1.FormPaint(Sender: TObject);
var
i: Integer;
begin
if TryStrToInt(rgCount.Items[rgCount.ItemIndex], i) then
for i:=1 to i do begin
Canvas.Pen.Color:=Random($1000000);
Canvas.Brush.Color:=Random($1000000);
case rgShape.Items[rgShape.ItemIndex] of
'Lines': Canvas.Line(Random(ClientWidth), Random(ClientHeight), Random(ClientWidth), Random(ClientHeight));
'Rectangles': Canvas.Rectangle(Random(ClientWidth), Random(ClientHeight), Random(ClientWidth), Random(ClientHeight));
'Ellipses': Canvas.Ellipse(Random(ClientWidth), Random(ClientHeight), Random(ClientWidth), Random(ClientHeight));
end;
end;
end;
- start your program. An example of how it could look like is given on the screenshot to the right.
See also
- TRadioGroup doc
- TRadioButton - Single radio button elements
- TCheckBox - Alternative to radio buttons, representing an on/off state of a single option
- TCheckGroup - Similar to a TRadioGroup but with checkboxes as elements
- TCheckListBox - Alternative element to select several options in a list with checkmark boxes
- TPanel - General grouping element, usable to group TRadioButtons for example
- TGroupBox - General grouping of elements with a titled box
- TFlowPanel - General grouping element which provides an automatic flow layouting of its children
- TComboBox - Alternative element to select a single option from a drop-down list