Search code examples
delphitimercountdown

Simple Delphi TTimer Program


can anyone help me how to display a simple timer using TTimer component in Delphi? I have a label where will the countdown from 5-0 will be displayed. Please help. Just a simple one. Thanks


Solution

  • You don't say what the time interval between 5 and 0 is, but I'll assume that it's seconds. You need to define a global variable of type integer with an initial value of 5 (I'll call it 'countdown'), and a timer with an interval of 1000. Its OnTimer method will be as follows:

    Procedure Timer1Timer (sender: TObject);
    begin
     if countdown > 0 then
      begin
       dec (countdown);
       label1.caption:= inttostr (countdown)
      end
     else timer1.enabled:= false
    end;
    

    When you want the countdown to commence, enable the timer and set the label's caption to be '5'.