Loop function until the application closes

Asked

Viewed 542 times

1

I have the following code that keeps changing image from time to time.

if Timer1.enabled = true then
begin
 Gauge1.Progress := Gauge1.Progress +1;
   if Gauge1.Progress=10 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg');

   if Gauge1.Progress=30 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg');

   if Gauge1.Progress=50 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg');
 end;

How do I make it loop until the application is finished

4 answers

1


Let’s clarify your doubt... You can do it this way. Since you have already included a Timer component on the screen called Timer1.

1º - Create an Ontimer event from the Timer component.

2º - Put the code, looking like this.

procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;

    if Gauge1.Progress = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg')    
    else if Gauge1.Progress = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg')    
    else if Gauge1.Progress = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg');    
end;

3º - Set the time when the Gauge1.Progress will be incremented in Timer1 on the property Interval, remembering that time is measured in ms, that is, 1000 is 1 second. If it is 1 according to the time set, in 10 seconds the process will change the image to 2.jpg, in 30 seconds 3.jpg and so on.

4º - When you want to start this count just put the Timer1 Enabled property in True, and otherwise False.

5º - In this case from 50 the image will always be 1.jpg, let’s say when the Gauge1.Progress to get to 60 we want to start counting again from 0. Then we change the code and put something like this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;

    if Gauge1.Progress = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg')    
    else if Gauge1.Progress = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg')    
    else if Gauge1.Progress = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg')
    else if Gauge1.Progress = 60 then
        Gauge1.Progress = 0;     
end;

6th - Case o Gauge1.Progress cannot be reset, depending on the logic of your program, so create a variable in Form of type Integer to do this process. Getting something like:

private 
    contador: Integer;
...
...
procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;
    Inc(contador);

    if contador = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg')    
    else if contador = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg')    
    else if contador = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg')
    else if contador = 60 then
        contador = 0;
end;

1

Use a repeat structure for this, let’s try with while:

while Gauge1.Progress < 100 do
begin
  if (Gauge1.Progress = 10) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg');
  end
  else if (Gauge1.Progress = 30) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg');
  end
  else if (Gauge1.Progress = 50) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg');
  end;
  Inc(Gauge1.Progress);
end;

That is to say while the % does not reach 100% you will update, now you need to fit this structure so who does not stop your system to run it! This already depends on when you will run this implementation of Gauge.

0

Dude, you can basically use Ttimmer, put it in his event: onTimer,.

  • don’t need to use a tgauge to control? , if you can be a little more specific

  • No Timmer already has a loop, the onTimer

  • can edit your question and put an example of how it would look?

0

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    if Timer1.enabled = true then
    begin
        Gauge1.Progress := Gauge1.Progress +1;
        if Gauge1.Progress=10 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\2.jpg');

        if Gauge1.Progress=30 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\3.jpg');

        if Gauge1.Progress=50 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home\1.jpg');
    end;
end;

in the Interval property you set the loop time to milesseconds. Timer1.interval := 1000; //1 second

  • You just copied my code and didn’t help me at all.

  • If you look at the first line I put in the event onTimer, and in the last line I explained how the Timer component works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.