Recent

Author Topic: Undo value change on TRadioBox change?  (Read 2340 times)

MartinIsla

  • Newbie
  • Posts: 6
Undo value change on TRadioBox change?
« on: December 23, 2014, 07:04:51 pm »
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: [Select]
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;

Thanks!

Jkey

  • New Member
  • *
  • Posts: 44
Re: Undo value change on TRadioBox change?
« Reply #1 on: December 23, 2014, 07:55:43 pm »
If I understand correctly, you would like to get back the original value of precioBase variable, don't you?
What about this?
Code: [Select]
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;
This way you don't touch precioBase, so you can avoid finding out its original value.

MartinIsla

  • Newbie
  • Posts: 6
Re: Undo value change on TRadioBox change?
« Reply #2 on: December 23, 2014, 08:03:30 pm »
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: [Select]
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;

Do you like this?
« Last Edit: December 23, 2014, 08:07:26 pm by MartinIsla »

 

TinyPortal © 2005-2018