Cel :

Chcemy uzyskać efekt, aby po kliknięciu na przycisk był odliczany czas, aż do jego zatrzymania. Przypominac to będzie stoper


Końcowy efekt :


Potrzebne komponenty :


Metoda :

1) Wstawiamy komponenty, wypisane.w powyższej tabeli i zmieniamy im właściwość name na taką jaka jest w kolumnie "Nazwa"

2) Ustawiamy właściwości Caption komponentów Label na: 0, zaś komp. TButton na: Stop

3) Do obsługi funkcji OnTimer dodajemy poniższy kod:

X := StrToInt(Secundy.Caption) + 1;
if StrToInt(Secundy.Caption) = 59 then Begin
X := 0;
Y := Y + 1;
Minuty.Caption := IntToStr(Y);
end;
if StrToInt(Secundy.Caption) = 59 then Begin
if StrToInt(Minuty.Caption) = 59 then Begin
Y := 0;
Minuty.Caption := IntToStr(Y);
Z := Z + 1;
Godziny.Caption := IntToStr(Z);
end;
end;
Secundy.Caption := IntToStr(X);

4) Znajdujemy w kodzie napis implementation, i dodajemy pod nim deklaracje globalną zmiennych:

var
X,Y, Z : integer;

5) Do obsługi funkcji OnClick komp. Button dodajemy kod:

if Timer1.Enabled = True then begin
Timer1.Enabled := false;
Button1.Caption := 'Start';
end else begin
Timer1.Enabled := true;
Button1.Caption := 'Stop';
end;

6) Klikamy pojedyńczo w puste miejsce formularza i z zakładki Event wybieramy funkcję OnActive. Wpisujemy kod:

X := 0;
Y := 0;
Z := 0;

7) Uruchamiamy program.


Kod źródłowy :

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
Secundy: TLabel;
Minuty: TLabel;
Godziny: TLabel;
Button1: TButton;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public

{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
X,Y, Z : integer;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
X := StrToInt(Secundy.Caption) + 1;
if StrToInt(Secundy.Caption) = 59 then Begin
X := 0;
Y := Y + 1;
Minuty.Caption := IntToStr(Y);
end;
if StrToInt(Secundy.Caption) = 59 then Begin
if StrToInt(Minuty.Caption) = 59 then Begin
Y := 0;
Minuty.Caption := IntToStr(Y);
Z := Z + 1;
Godziny.Caption := IntToStr(Z);
end;
end;
Secundy.Caption := IntToStr(X);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Timer1.Enabled = True then begin
Timer1.Enabled := false;
Button1.Caption := 'Start';
end else begin
Timer1.Enabled := true;
Button1.Caption := 'Stop';
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
X := 0;
Y := 0;
Z := 0;
end;

end.