Forum > Beginners

Undo value change on TRadioBox change?

(1/1)

MartinIsla:
Hi everyone!

I have a pretty simple problem, but I don't really know how to fix it.

I have a TRadioBox, and when I select an option, it increments the value of a variable by X depending on the item selected.

The problem is: if the user changes the chosen option, it increments the value of the var, but it doesn't deduct the previous added value. Is there a way of doing it automatically? If not, how can I fix it?


--- Code: ---procedure TForm4.RadioGroup1SelectionChanged(Sender: TObject);
begin
  if RadioGroup1.ItemIndex = 0 then
     precioBase := precioBase + 20000 else
  if RadioGroup1.ItemIndex = 1 then
     precioBase := precioBase + 20000 else
  if RadioGroup1.ItemIndex = 2 then
     precioBase := precioBase + 10000 else
  if RadioGroup1.ItemIndex = 3 then
     precioBase := precioBase + 5000 else
  if RadioGroup1.ItemIndex = 4 then
     precioBase := precioBase + 5000;
  costo.caption := FloatToStr(precioBase);

end;
--- End code ---

Thanks!

Jkey:
If I understand correctly, you would like to get back the original value of precioBase variable, don't you?
What about this?

--- Code: ---procedure TForm4.RadioGroup1SelectionChanged(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of
    0: costo.caption := FloatToStr(precioBase + 20000);
    1: costo.caption := FloatToStr(precioBase + 20000);
    2: costo.caption := FloatToStr(precioBase + 10000);
    3: costo.caption := FloatToStr(precioBase + 5000);
    4: costo.caption := FloatToStr(precioBase + 5000);
  end;
end;
--- End code ---
This way you don't touch precioBase, so you can avoid finding out its original value.

MartinIsla:
That's great! It wouldn't really work because I have other RadioBoxes and CheckBoxes that modify that value, BUT thanks to you, I realised I could use auxiliar variables so I don't touch the original, and put them all together once the button is pressed!

Thank you  :)


--- Code: ---procedure TForm4.RadioGroup1SelectionChanged(Sender: TObject; var addRG1:integer);
begin
  case RadioGroup1.Index of
  0: addRG1 := 20000;
  1: addRG1 := 20000;
  2: addRG1 := 10000;
  3: addRG1 := 5000;
  4: addRG1 := 5000;
  end;
  costo.caption := FloatToStr(precioBase + addRG1);
end;
--- End code ---

Do you like this?

Navigation

[0] Message Index

Go to full version