Is it possible to show the countdown of a TTimer in a Label ? Like instantly putting the variable in the Label Caption. I was thinking about how I could do, I'm trying to do a visible countdown in the Form.
As Ken White said, a TTimer
doesn't have a 'countdown'. However, of course it is possible to implement a 'countdown' in your application. The following is an example of one way of doing this.
Create a new VCL application.
Add a private integer variable named FCount
to your form class.
Use the following code as your form's OnCreate
event handler:
procedure TForm1.FormCreate(Sender: TObject);
begin
FCount := 10;
Randomize;
end;
OnPaint
event handler:
procedure TForm1.FormPaint(Sender: TObject);
var
R: TRect;
S: string;
begin
Canvas.Brush.Color := RGB(Random(127), Random(127), Random(127));
Canvas.FillRect(ClientRect);
R := ClientRect;
S := IntToStr(FCount);
Canvas.Font.Height := ClientHeight div 2;
Canvas.Font.Name := 'Segoe UI';
Canvas.Font.Color := clWhite;
Canvas.TextRect(R, S, [tfCenter, tfSingleLine, tfVerticalCenter]);
end;
TTimer
to your form, and use the following code as its OnTimer
handler:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if FCount = 0 then
begin
Timer1.Enabled := false;
MessageBox(Handle, 'Countdown complete.', 'Countdown', MB_ICONINFORMATION);
Close;
end
else
begin
Invalidate;
dec(FCount);
end;
end;
Call the Invalidate
method in the form's OnResize
handler.
Run the application.