Delphi : Membuat Countdown Timer Beserta Progressbar
Sebelumnya saya mohon maaf yang sebesar-besarnya, atas kekhilafan dan kekurangan ilmu saya karena salah dalam menuliskan kalimat bassmallah pada setiap postingan saya. Semoga Allah SWT mengampuni kekhilafan saya karena keawwaman saya. Aamiin
1. Deklarasikan variabel ITime sebagai variabel menyimpan waktu sekarang
var
Form1: TForm1;
ITime : TTime;// Variabel
implementation
Form1: TForm1;
ITime : TTime;// Variabel
implementation
2. Pada tombol mulai, ketikkan kode berikut
procedure TForm1.Button1Click(Sender: TObject);
var
h,m,s : word;
sec,jam,menit,detik : word;
incs : word;
begin
ProgressBar1.Position := 0;
h := HourOf(DTWaktu.Time);
m := MinuteOf(DTWaktu.Time);
s := SecondOf(DTWaktu.Time);
incs := (h*Sqr(60)) + (m*60) + s;
ITime := Now();
ITime := IncSecond(ITime,incs);
sec := SecondsBetween(Now(),ITime);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
ProgressBar1.Max := sec;
Button1.Enabled := false;
Button3.Enabled := true;
Timer1.Enabled := true;
end;
var
h,m,s : word;
sec,jam,menit,detik : word;
incs : word;
begin
ProgressBar1.Position := 0;
h := HourOf(DTWaktu.Time);
m := MinuteOf(DTWaktu.Time);
s := SecondOf(DTWaktu.Time);
incs := (h*Sqr(60)) + (m*60) + s;
ITime := Now();
ITime := IncSecond(ITime,incs);
sec := SecondsBetween(Now(),ITime);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
ProgressBar1.Max := sec;
Button1.Enabled := false;
Button3.Enabled := true;
Timer1.Enabled := true;
end;
3. Kemudian pada tombol stop, ketikkan kode berikut:
procedure TForm1.Button3Click(Sender: TObject);
begin
ProgressBar1.Position := 0;
DTWaktu.Time := EncodeTime(0,0,10,0);
Timer1.Enabled := false;
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
Label3.Caption := '-||-';
end;
begin
ProgressBar1.Position := 0;
DTWaktu.Time := EncodeTime(0,0,10,0);
Timer1.Enabled := false;
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
Label3.Caption := '-||-';
end;
4. Ketikkan kode berikut pada event FormOnCreate
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := false;
Timer1.Interval := 500;
end;
begin
Timer1.Enabled := false;
Timer1.Interval := 500;
end;
5. Tambahkan komponen TTimer, pada event OnTimer ketikkan kode berikut:
procedure TForm1.Timer1Timer(Sender: TObject);
var sec,jam,menit,detik : word;
beginif ProgressBar1.Position = ProgressBar1.Max then
begin
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
end
else
begin
sec := SecondsBetween(Now(),ITime);
ProgressBar1.Position := ProgressBar1.Max - (sec -1);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
end;
end;
var sec,jam,menit,detik : word;
beginif ProgressBar1.Position = ProgressBar1.Max then
begin
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
end
else
begin
sec := SecondsBetween(Now(),ITime);
ProgressBar1.Position := ProgressBar1.Max - (sec -1);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
end;
end;
Kode selengkapnya sebagai berikut:
unit uUtama;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, ComCtrls, ExtCtrls, StdCtrls, ShellApi;
type
TForm1 = class(TForm)
DTWaktu: TDateTimePicker;
Label1: TLabel;
Button1: TButton;
ProgressBar1: TProgressBar;
Label2: TLabel;
Button2: TButton;
Label3: TLabel;
Timer1: TTimer;
Button3: TButton;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Label4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, ComCtrls, ExtCtrls, StdCtrls, ShellApi;
type
TForm1 = class(TForm)
DTWaktu: TDateTimePicker;
Label1: TLabel;
Button1: TButton;
ProgressBar1: TProgressBar;
Label2: TLabel;
Button2: TButton;
Label3: TLabel;
Timer1: TTimer;
Button3: TButton;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Label4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ITime : TTime;// Variabel
implementation
uses DateUtils;//Uses ini dulu
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
h,m,s : word;
sec,jam,menit,detik : word;
incs : word;
begin
ProgressBar1.Position := 0;
h := HourOf(DTWaktu.Time);
m := MinuteOf(DTWaktu.Time);
s := SecondOf(DTWaktu.Time);
incs := (h*Sqr(60)) + (m*60) + s;
ITime := Now();
ITime := IncSecond(ITime,incs);
sec := SecondsBetween(Now(),ITime);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
ProgressBar1.Max := sec;
Button1.Enabled := false;
Button3.Enabled := true;
Timer1.Enabled := true;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ProgressBar1.Position := 0;
DTWaktu.Time := EncodeTime(0,0,10,0);
Timer1.Enabled := false;
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
Label3.Caption := '-||-';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := false;
Timer1.Interval := 500;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
sec,jam,menit,detik : word;
begin
if ProgressBar1.Position = ProgressBar1.Max then
begin
Timer1.Enabled := false;
Button1.Enabled := true;
Button3.Enabled := false;
end
else
begin
sec := SecondsBetween(Now(),ITime);
ProgressBar1.Position := ProgressBar1.Max - (sec -1);
jam := sec div 3600;
menit := (sec mod 3600) div 60;
detik:= (sec mod 3600) mod 60;
Label3.Caption := 'Sisa waktu : '+IntToStr(jam)+' jam '+IntToStr(menit)+' menit '+IntToStr(detik)+' detik';
end;
end;
end.
Unduh demo program di sini
thanks thanks ^^
ReplyDeleteini yang bener dibutuhin saya saat ini ..
:D
:) terimkasih sangat bermanfaat
ReplyDelete