Here are the steps to accomplish this.
- Create your two RadioButtons, call it
RadioButton1
and RadioButton2
.
- Set their GroupName property to the same string for both radio buttons.
- Right click your 1st radio button and select Bind Visually...
- In the LiveBindings designer, right click your radio button and select
Bindable Members
, and then select the checkbox IsChecked
followed by clicking the ok button.
- Still within the Live Bindings designer, Now drag a link between the
IsChecked
property and the field you wish to bind to (note this can be a string field).
- Repeat steps 4 and 5 for the other Radio Button.
Now you are almost down, but you need to convert the string to a boolean so that the IsChecked
property will have a boolean value. To do this, select the binding link from the LiveBindings Designer for your radio button. Then in its CustomFormat
property, assign the following string
IfThen(ToStr(%s)="Poor",True, False)
This will allow the radio button to be checked when the underlying database value is 'Poor'
Do the same for your other radio button, except use a different string
IfThen(ToStr(%s)="Excellent",True, False)
Now to give the radio buttons the ability to change the underlying database field, you will need to attach code to perform this. Let us use the radio button's OnClick
event (attach to both radio buttons). This code assumes your underlying dataset is named FDCustomer
, and your field is named Status
. Note that the radio button is not checked yet at the time of the event, so we look for IsChecked
to be false.
if Sender = RadioButton1 then
begin
if not TRadioButton(Sender).IsChecked then // checking
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Poor';
end;
end
else if Sender = RadioButton2 then
begin
if not TRadioButton(Sender).IsChecked then
begin
fdcustomer.Edit;
fdcustomer.FieldByName('Status').AsString:= 'Excellent';
end;
end;