Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create a countdown timer Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Algorithm
Language
Delphi 2.x
Views
111
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I was wondering if anyone knew of a way in which you were able to create a timer in 
which you could set a time (of about 20 minutes) and have it countdown by seconds 
and be able to stop it.

Answer:

Drop a TTimer onto your form. Set the INTERVAL using the object inspector to 1000. 
Set the control's Enabled property to False. Use another control, say a TEdit or 
TSpinEdit to set a variable with the total number of seconds you wish to wait. Use 
a TButton control to enable the timer. Use a second button to disable the timer. 
Double-click on the timer to create an OnTimer event handler. In the event handler, 
decrement the total time counter and check to see if it hit zero.
1   
2   procedure TForm1.Edit1Change(Sender: TObject);
3   begin
4     {the time is entered in seconds.  If you wish the time to be entered in 
5   "hh:mm:ss",
6     you will have to parse it and put it into a total seconds format.}
7     TotalTime := StrToInt(Edit1.Text);
8   end;
9   
10  procedure TForm1.Button1Click(Sender: TObject);
11  begin
12    Timer1.Enabled := true;
13    Edit1.Enabled := false; {disable the ability to set the time}
14  end;
15  
16  procedure TForm1.Button2Click(Sender: TObject);
17  begin
18    Timer1.Enabled := false;
19    Edit1.Enabled := true; {re-enable the ability to set the time}
20  end;
21  
22  procedure TForm1.Timer1Timer(Sender: TObject);
23  begin
24    dec(TotalTime); {decrement the total time counter}
25    Edit2.Text := IntToStr(TotalTime); {put the value in an edit box so he can see it}
26    if TotalTime = 0 then {have we timed out?}
27      {... Do something ...}
28  end;


Remark:

Rather than decrement a counter in the OnTimer event handler, it's better to compare the current system time to the original start time and calculate the difference. The reason for this is that timer messages are low priority, and it's very likely that some will be lost before being processed, causing any countdown scheme to be inaccurate.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC