Re: Unable to do a repeat calculation without closing down and starting up again.


[ Delphi Forum ]

Posted by Lionel on February 01, 2004 at 02:07:26:

In Reply to: Re: Unable to do a repeat calculation without closing down and starting up again. posted by webmaster Guido on January 31, 2004 at 20:10:15:

: : The following code is all on one form and is an example of what I am trying to convey as my problem.
: : The top part of the form has a Calculate, Clear and Close Button with two Edit Boxes and a Results display label.
: :
: : The bottom part of the form has the same set up, a Calculate , Clear and Close Button.
: :
: : I don't know if I am making much sense with this but I can enter a number in Edit box 1 and multiply it by another number in Edit box 2 and read the results, I can then do the same thing in the second half of the page using Edit box 3 and Edit box 4 and read the results.
: :
: : The problem: I clear the Edit boxes and try to enter new data and repeat the operation but nothing happens ? -- on occasions I get a wrong answer which makes me think I am not erasing anything with the Clear buttons and old data is getting involved in the second attemp to make a calculation. I hope this explains my code problem if not, let me know.
: :
: : procedure TForm1.Button1Click(Sender: TObject);
: : VAR
: : Num1,
: : Num2,
: : ANS : Variant;
: : begin
: : Num1:=Edit1.Text;
: : Num2:=Edit2.Text;
: : //-----------------
: : ANS:= Num1 * Num2;
: : //-------------------
: : Label7.Caption:=FloatToStr(ANS);
: : //-----------------
: : end;

: : procedure TForm1.Button2Click(Sender: TObject);
: : begin
: : Edit1.Clear;
: : Edit2.Clear;
: : label7.visible:=False;
: : end;

: : procedure TForm1.Button3Click(Sender: TObject);

: : VAR
: : Num3,
: : Num4,
: : ans : Variant;

: : begin
: : Num3:=(Edit3.Text);
: : Num4:=(Edit4.Text);
: : //---------------------
: : ans:=Num3*Num4;
: : //-------------------
: : Label8.Caption:=FloatToStr(ans);

: : end;

: : procedure TForm1.Button4Click(Sender: TObject);
: : begin
: : Edit3.Clear;
: : Edit4.Clear;
: : //-----------
: : Label8.Visible:=false;
: : end;
: ------------------------------------------

: When clearing the results, you make the labels invisible, so you can't see the result of the next calculation. So, instead of:

: Label7.Visible := False;
: and
: Label8.Visible := False;

: use this:

: Label7.Caption := '';
: and
: Label8.Caption := '';

: I've tested your code with these modifications, using the same names for variables and components. It never shows wrong results on my PC.

: Please, do as I suggested earlier, and copy everything into a small testprogram, to see if you can reproduce the errors.

+++++++++++++++++++++++++++++++++++++++++

Thanks, I will take your advice and try the small testprograms in the future.
I change all my Label in my project and it is working great.


Related Articles and Replies:


[ Delphi Forum ]