Stopwatch

How do you create a simple stopwatch on a form (00-00-00) type...stop,go reset???
SendoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

isasoriCommented:
Use Timer component from System,
Label, buttons.
Interval property 1000 - 1 sec,
100 - 1/10 sec.

Sample:


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Timer: TTimer;
    Label1: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure TimerTimer(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    h,m,s:string;
    procedure showtime;

    { Private declarations }
  public
    hours,minutes,seconds:Byte
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.TimerTimer(Sender: TObject);
begin
  if seconds<59 then
    seconds:=seconds+1
  else
    if minutes<59 then begin
      minutes:=minutes+1;
      seconds:=0
    end
       else if hours<99 then begin
         hours:=hours+1;
         Minutes:=0;
         seconds:=0;
       end
         else begin
         hours:=0;
         Minutes:=0;
         seconds:=0;
       end;
  showtime;

  {Label1:=}
end;

procedure TForm1.showtime;
begin
    str(seconds,s);
    if seconds<10 then s:='0'+s;
    str(minutes,m);
    if minutes<10 then m:='0'+m;
    str(hours,h);
    if hours<10 then h:='0'+h;
  Label1.Caption:=h+'-'+m+'-'+s;

end;
procedure TForm1.Button3Click(Sender: TObject);
begin
  Hours:=0;
  Minutes:=0;
  Seconds:=0;
  showtime;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer.Enabled:=True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer.Enabled:=False;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Timer.Enabled:=False;
  Form1.Button3Click(Owner);

end;

end.



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BatalfCommented:
Just a little comment :
Is there a possibility to make the stopwatch show 1/100 sec ?
0
craig_capelCommented:
yes i can do it
brb with answer, just creating it...
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

craig_capelCommented:
unit timer1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
    loopfinish: boolean;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  t,num: real;
begin
    t:=gettickcount;
    loopfinish:=false;
    repeat
      num:=(gettickcount-t)/1000;
      edit1.text:=floattostr(num);
      application.processmessages;

    until loopfinish=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    loopfinish:=true;
end;

end.

stick a start and a stop button on the form, then put an edit box on, and this is what i think your after....
0
isasoriCommented:
My example of a stopwatch shows hours, minute, second in a format hh-mm-ss.
Timer.Interval property is equal 1000.

To show 1/100 seconds it is necessary to add an item "-zz" in Label and to count them similarly to count of hours, Interval property = 10.

( For 1/10 it's necessary to add only
"-z" and to count up to 9, Interval property = 100).
0
craig_capelCommented:
yes but thats more of a fast clock than a real stopwatch....
0
SendoAuthor Commented:
How do I pass stopwatch info to another Form (F2)?
Thanx!
0
craig_capelCommented:
easily done.....  http://members.xoom.com/craig_c/

oh you want to know how? but you ignored my comment
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.