1
votes

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!

1

1 Answers

2
votes

Here are the steps to accomplish this.

  1. Create your two RadioButtons, call it RadioButton1 and RadioButton2.
  2. Set their GroupName property to the same string for both radio buttons.
  3. Right click your 1st radio button and select Bind Visually...
  4. 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.
  5. 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).
  6. 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;