I have a form with 2 RadioButtons
(with same GroupName) and I need to save 'A'
(if RadioButton1 is selected) or 'I'
(if RadioButton2 is selected) in the field Status using LiveBindings.
One Component to One Field is easy, but in this case I have two components getting and setting values from one field.
I created a function that returns the radiobutton
selecting through Groupname
and fill the field manually, but I wanted something more automatic.
Thanks in advanced!
Here are the steps to accomplish this.
RadioButton1
and RadioButton2
.Bindable Members
, and then select the checkbox IsChecked
followed by clicking the ok button. IsChecked
property and the field you wish to bind to (note this can be a string field).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;