Bryan
Bryan

Reputation: 61

Delphi Timer issue

procedure TForm1.Timer1Timer(Sender: TObject);
var
   i : integer;
begin
  if i > StrToInt(Edit1.Text) then
    Timer1.Enabled := False
  else
    i :=+ 1;
  SendClick(645,302);
  Sleep(2200);
  SendClick(694,619);
  Sleep(2200);
  SendClick(967,638);
  Sleep(2200);
  SendKeys('{BKSP}{BKSP}{BKSP}{BKSP}1',False);
  SendClick(917,688);
  Sleep(2200);
  SendClick(917,688);
  Sleep(2200);
  SendClick(917,688);
  amount := StrToInt(Label3.Caption) + 1;
  Label3.Caption := IntToStr(amount);
end;

for some reason it repeats only 1 time and stops... can anyone spot a problem? im pretty tired and ive went over and over it a few times and i can't seem to see one...

Upvotes: 1

Views: 685

Answers (4)

APZ28
APZ28

Reputation: 1007

This is a well case that people just ignore the Warning message. I wish that compiler should spit out Hint or Error and No Warning. Warning is just a case that short comming from compiler and it should be fixed at later version.

Cheers

Upvotes: 0

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108948

The line

i :=+ 1;

assigns the value +1 (also known as 1) to the variable named i. (That is, if i is equal to 55, and you do i :=+ 1, then i will be equal to 1.)

Perhaps you are seeking

i := i + 1;

or

inc(i);

?

Upvotes: 10

jachguate
jachguate

Reputation: 17203

I is a uninitialized local variable (it contains garbage), so the result of the comparision if i > StrToInt(Edit1.Text) is random.

You may want to add a member variable to your form's class, initialize at the proper time and check it's value on the onTimer event, something like:

type
  TForm1 = class(TForm)
    ..
  private
    FTimerCount: Integer;
    FMaxTimerCount: Integer;
    ..


procedure TForm1.Button1Click(Sender: TObject);
begin
  FTimerCount := 0;
  FMaxTimerCount := 20;  //the timer will fire 20 times.
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Inc(FTimerCount);
  Timer1.Enabled := FTimerCount < FMaxTimerCount;
  DoOtherStuff();
end;

Upvotes: 11

Sertac Akyuz
Sertac Akyuz

Reputation: 54802

You're not initializing i, it is a local variable. Hence the timer can be enabled or not depending on the arbitrary value it's memory location holds.

Upvotes: 5

Related Questions