Re: Countdown with timer

Posted by Bjorn on May 13, 2007

In Reply to Countdown posted by Stuart Hopkins on May 05, 2007

: I need from some expert Delphi help please. I have a timer on my form that I want to countdown from 20:00, in seconds and minutes, until the clock reaches 00:00. Any help would be fantastic thanks.

You need an integer variable that keeps the remaining time in seconds:

var Seconds: integer;

At design time, set the Timer's property Interval to 1000 and set property Enabled to false.

At run time, when you start the Timer, for example by clicking a button:

Seconds := 1200;
Timer1.Enabled := True;
Label1.Caption := '20:00';

Source code example for the event handler for the Timer:

var Min, Sec: string;
begin
  if Seconds = 0 then begin
    Timer1.Enabled := False;
    ShowMessage('Done!');
  end 
  else begin
    dec(Seconds);
    Min := IntToStr(Seconds div 60); // integer division
    Sec := IntToStr(Seconds mod 60); // remainder
    if Length(Min) = 1 then Min := '0' + Min;
    if Length(Sec) = 1 then Sec := '0' + Sec;
    Label1.Caption := Min + ':' + Sec;
  end;
end;

Good luck!
Bjorn

Related articles

       

Follow Ups