unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
DateUtils;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure CreateTimerNormal;
procedure timCountdownTimer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
TimeOut: TDateTime;
NewTimer: TTimer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.CreateTimerNormal;
begin
if not Assigned(NewTimer) then begin
NewTimer := TTimer.Create(Self);
with NewTimer do
begin
Interval := 1000;
OnTimer := @timCountdownTimer;
Enabled := False;
end;
end;
end;
function SecsToHmsStr(ASecs: integer):string;
begin
Result := Format('%2d:%2.2d:%2.2d',
[ASecs div 3600, ASecs mod 3600 div 60, ASecs mod 3600 mod 60]);
;end;
procedure TForm1.timCountdownTimer(Sender: TObject);
begin
Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut));
if Now > Timeout then NewTimer.Enabled := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Seconds: integer;
begin
CreateTimerNormal;
if TryStrToInt(Edit1.Text, Seconds) then
begin
TimeOut := IncSecond(Now, Seconds);
NewTimer.Enabled := True;
Label1.Caption := SecsToHmsStr(SecondsBetween(Now, TimeOut));
end
else
ShowMessage('Error in number of seconds');
end;
end.