Delphi Tutorial Timer

In this video you can learn how to work with Timer in Delphi.
You can learn how to manipulate with components using timer.

example application:https://drive.google.com/open?id=0B_njK7HczCjDOEpVMER4aFctSDQ

Here is also code used in this video :



(code style formatted by http://hilite.me/ )



unit MainForm;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Image1: TImage;
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    ProgressBar1: TProgressBar;
    Timer1: TTimer;
    Timer2: TTimer;
    Timer3: TTimer;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Edit1: TEdit;
    UpDown1: TUpDown;
    Edit2: TEdit;
    UpDown2: TUpDown;
    Edit3: TEdit;
    UpDown3: TUpDown;
    Edit4: TEdit;
    UpDown4: TUpDown;
    Edit5: TEdit;
    UpDown5: TUpDown;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown2Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown3Click(Sender: TObject; Button: TUDBtnType);
    procedure UpDown4Click(Sender: TObject; Button: TUDBtnType);
    procedure Button7Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Edit5Change(Sender: TObject);
    procedure UpDown5Click(Sender: TObject; Button: TUDBtnType);
  private
    { Private declarations }
  public
   LabInt:Integer;
   ButtonTag: Integer;

   IntxL, IntxR, IntxB, IntxT : Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if Timer1.Enabled=false then
  Timer1.Enabled:=true
else
  Timer1.Enabled:=False;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if Timer2.Enabled=false then
  Timer2.Enabled:=true
else
  Timer2.Enabled:=False;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ButtonTag:=101;
Timer3.Enabled:=True;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
ButtonTag:=102;
Timer3.Enabled:=True;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
ButtonTag:=103;
Timer3.Enabled:=True;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
ButtonTag:=104;
Timer3.Enabled:=True;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Timer1.Enabled:=False;
Timer2.Enabled:=False;
Timer3.Enabled:=False;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
IntxL:=StrToInt(Edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
IntxR:=StrToInt(Edit2.Text);
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
IntxT:=StrToInt(Edit3.Text);
end;

procedure TForm1.Edit4Change(Sender: TObject);
begin
IntxB:=StrToInt(Edit4.Text);
end;

procedure TForm1.Edit5Change(Sender: TObject);
begin
Timer3.Interval:=StrToInt(Edit5.Text) ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

LabInt:=0;

IntxL :=10;
IntxR :=10;
IntxB:=10;
IntxT:=10;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
LabInt:=LabInt+10;
Label1.Caption:=IntToStr(LabInt);
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if ProgressBar1.Position<ProgressBar1.Max then
 ProgressBar1.Position:=ProgressBar1.Position+1
else
  ProgressBar1.Position:=0;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
begin
case ButtonTag of
101:
if Image1.Left>0 then
Image1.Left:=Image1.Left-IntxL
else Button4.Click;
102:
if Image1.Left<Panel2.Width then
Image1.Left:=Image1.Left+IntxR
else
Button3.Click;
103:
if Image1.Top>0 then
Image1.Top:=Image1.Top-IntxT
else
Button6.Click;
104:
if Image1.Top<Panel2.Height then
 Image1.Top:=Image1.Top+IntxB
 else
 Button5.Click;

end;

end;

procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxL:=IntxL+10;
if Button=btPrev then IntxL:=IntxL-10;
Edit1.Text:=IntToStr(IntxL);
end;

procedure TForm1.UpDown2Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxR:=IntxR+10;
if Button=btPrev then IntxR:=IntxR-10;
Edit2.Text:=IntToStr(IntxR);
end;

procedure TForm1.UpDown3Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxT:=IntxT+10;
if Button=btPrev then IntxT:=IntxT-10;
Edit3.Text:=IntToStr(IntxT);
end;

procedure TForm1.UpDown4Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then IntxB:=IntxB+10;
if Button=btPrev then IntxB:=IntxB-10;
Edit4.Text:=IntToStr(IntxB);
end;

procedure TForm1.UpDown5Click(Sender: TObject; Button: TUDBtnType);
begin
if Button=btNext then Timer3.Interval:=Timer3.Interval+10;
if Button=btPrev then Timer3.Interval:=Timer3.Interval-10;
Edit5.Text:=IntToStr(Timer3.Interval);
end;

end.
Share on Google Plus

About Kn Kn

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.