Looping TGauge with timer

2.1k views Asked by At

I want to replace my progress bar with gauge. Here is version on progress bar:

procedure TForm1.tmr1Timer(sender: TObject);
begin
  pb0.Position := (pb0.Position + 1) mod pb0.Max;
end;

And this is on gauge

procedure TForm1.tmr1Timer(sender: TObject);
begin
  gauge.MinValue := 0;
  gauge.MaxValue := 100;
  gauge.Progress := gauge.Progress + 1;
end;

How to make it start over every time it reach 100 ? As i tried with button to test, i can not make it looping just like on progress bar.

procedure TForm1.btn6Click(sender: TObject);
begin
  tmr1.Enabled := not tmr1.Enabled;
  begin
    gauge.Progress := 0;
    tmr1.Enabled := True
  end;
  if Form1.gauge.Progress = 100 then // is this correct ?
  // what to do to make it looping ?
end;

How to make same the function on gauge as replacement for progress bar + timer above?

1

There are 1 answers

7
Ken White On BEST ANSWER

The same way. Just use the different property names for TGauge (and remove the setting of MinValue and MaxValue from the timer event):

procedure TForm1.tmr1Timer(sender: TObject);
begin
  gauge.Progress := (gauge.Progress + 1) mod (gauge.MaxValue - gauge.MinValue);;
end;

@DavidHeffernan points out in a comment that my calculation will never reach a full 100% value, and suggests an alternative:

gauge.Progress := gauge.MinValue + (gauge.Progress + 1) mod 
             (gauge.MaxValue - gauge.MinValue + 1);

It has different issues: the progress display does not start at 0, and it increments in steps of 2. However, it indeed does reach 100%.

As @TLama pointed out in a comment, neither of the above work if the MinValue can be negative.


This calculation has no issues (loops from MinValue to MaxValue, and both can be negative) if MinValue < MaxValue

gauge.Progress := gauge.MinValue + 
                  ( gauge.Progress + 1 - gauge.MinValue ) mod 
                  ( gauge.MaxValue - gauge.MinValue + 1 );