Cronometro usando Ttimer.

Delphi

26/04/2022

Bom dia, sou iniciante em Delphi e gostaria de saber se é possivel fazer um contador no qual eu especificaria o tempo em uma edit e ao acionar o botao ele contaria até o tempo inserido depois pararia, alguém sabe me dizer como fazer isso? e ao parar ele pode exibir uma ShowMessage ? Obrigado desde já!
Paulo Oliveira

Paulo Oliveira

Curtidas 0

Melhor post

Natanael Ferreira

Natanael Ferreira

27/04/2022

Coloque 1 timer, 1 label para mostrar o tempo passando, 1 edit para definir o tempo limite e button para acionar o cronometro.

Declare a variavel global maxtime:

var
  maxtime: real;


No onshow do form desative o Timer:

 Timer1.Enabled := False;


No OnClick do button acione o cronometro:

maxtime := strtotime('00:00:00');
  Timer1.Enabled := True;


No Ontimer do Timer o procedimento para correr o tempo:

  maxtime := maxtime + strtotime('00:00:01');

  Label1.Caption := timetostr(maxtime);

  Application.ProcessMessages;

  if timetostr(maxtime) = Edit1.Text then
  begin
    Timer1.Enabled := False;
    ShowMessage('Tempo finalizado!');
  end;


Código completo:

unit Unit13;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm13 = class(TForm)
    Timer1: TTimer;
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form13: TForm13;
  maxtime: real;

implementation

{$R *.dfm}

procedure TForm13.Button1Click(Sender: TObject);
begin
  maxtime := strtotime('00:00:00');
  Timer1.Enabled := True;
end;

procedure TForm13.FormShow(Sender: TObject);
begin
  Timer1.Enabled := False;
end;

procedure TForm13.Timer1Timer(Sender: TObject);
begin
  maxtime := maxtime + strtotime('00:00:01');

  Label1.Caption := timetostr(maxtime);

  Application.ProcessMessages;

  if timetostr(maxtime) = Edit1.Text then
  begin
    Timer1.Enabled := False;
    ShowMessage('Tempo finalizado!');
  end;
end;

end.



GOSTEI 1

Mais Respostas

Paulo Oliveira

Paulo Oliveira

26/04/2022

Boa tarde amigo, eu havia feito um codigo parecido com o seu, e tive o mesmo problema, ele nao esta parando no tempo digitado, esta passando direto, voce tem ideia do que pode ser?
GOSTEI 0
Paulo Oliveira

Paulo Oliveira

26/04/2022

ESTA FUNCIONANDO!!! Obrigado!!
GOSTEI 0
POSTAR