Topic: StopWatch

Could somebody code a stopwatch please?
Please see the attached sample project.

Post's attachments

Attachment icon StopWatch.zip 6 kb, 420 downloads since 2017-11-17 

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Hi Adam,
I did something like a stop-watch a while back.
In my example, the stop watch starts as soon as you enter the number of seconds and similarly, it is reset when the number of seconds are deleted so I appreciate that it's not exactly as per your attachment but you should still be able to take the code for the basic stop-watch functionality and apply it to your project.
Hope it helps to move you forward.
Regards,
Derek.

Post's attachments

Attachment icon stopwatch.zip 335.97 kb, 436 downloads since 2017-11-18 

Re: StopWatch

Adam,
I've not ever done a project like this before, so I thought I would make an attempt at it. But I see Derek also put together something. Thanks Derek. Both are relatively close in concept and either technique could be used. I don't think there's ever just one way to skin a cat, so to speak.


But before you download and run, let me explain a couple of things. I tried to implement your split second counter (less than a second), but the calc and display of the tenths of a second slows the clock to the point where it becomes inaccurate. I tried to play with the interval, but without success.If it's changed to 100, it is way too slow. If it's changed to 93, it is too fast, If it's changed to 94, it is too slow. So the recommendation is to eliminate that part of the stopwatch and just use the seconds. It works fine and is accurate. I kept the display and script for the tenths of a second, but the script is commented. If you want to try it for yourself, then just uncomment the lines in the OnTimer procedure and change the interval to 100 or whatever. I think I have a commented line for that. Otherwise I would simply remove the display and script for it altogether.


I only placed the script for the first form (large display). I did not do anything with the other forms. I figured you could handle that.

Post's attachments

Attachment icon StopWatch Coded.zip 583.95 kb, 425 downloads since 2017-11-18 

Re: StopWatch

Hi Derek,
Thank you very much... Truly appreciated....
As always a nice alternative.


Hi EHW,
Thank you very much... Truly appreciated....
Almost there. Somebody did a StopWatch with Delphi. I wanted do MVD version. Please see the attached Delphi source if it helps with miliseconds etc. I'm far away from handling Delphi code in MVD.

Post's attachments

Attachment icon StopwatchSrc.zip 31.69 kb, 412 downloads since 2017-11-18 

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Hello AD1408, Derek and ehwagner

Why do not use Delphi  function GetTickCount ?
There are many pieces of code on tne net

JB

Re: StopWatch

Up until I looked through the Delphi code shared by Adam I had no idea what GetTickCount was or how to use it. Now I know. So utilizing the code in the Delphi source, I put it together in MVD. Check it out. It was a learning experience for me so thanks for that.

Post's attachments

Attachment icon StopWatch New.zip 584.44 kb, 462 downloads since 2017-11-20 

Re: StopWatch

Hello ehwagner

Happy to have introduced you to this old  feature of Delphi.
I added these three lines between BEGIN and END. to avoid flickering when refreshing the display each time the stopwatch is changed.

BEGIN
    Form1.DoubleBuffered           := True;
    frmMedium.DoubleBuffered   := True;
    frmSmall.DoubleBuffered       := True;
END.

Have a good day

JB

Re: StopWatch

Thank you very much EHW.......


I get an error message "Overflow while converting variant of type (LongWord) into type (integer)" on click of Start button on Form1.


Ps/.
I changed frmLarge name back to Form1 and added script for checkboxes so that they behave like radio buttons. Please see the sample project attached.
Thank you for economizing on visible buttons.

Post's attachments

Attachment icon StopWatch EHW 2.zip 8.4 kb, 421 downloads since 2017-11-20 

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Thank you Jean. Didn't know about DoubleBuffered either. I do not have a Delphi background and without real documentation, it can be sometimes difficult to know what can and cannot be done in MVD in certain situations. It's a good thing that this forum is here.


Adam, I downloaded your updated project, but I do not get any errors. Not sure what to tell you.

Re: StopWatch

Adam, I downloaded your updated project, but I do not get any errors. Not sure what to tell you.



Hi EHW,


I tried compiling with MVD 3.6 and MVD 4b beside MVD 3.5 but I still get the following error message on click of Start button.

https://s18.postimg.org/wqa0b7ey1/zzzzz_Temp71.png

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Adam,
I'm running MVD 3.6 and it works fine. I ran your project "Stopwatch EHW 2.zip" with no problems. Did the version I submitted "Stopwatch New.zip" work at all for you?

Re: StopWatch

Did the version I submitted "Stopwatch New.zip" work at all for you?

No, unfortunately it produced the same error too.

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

You should use type Cardinal instead Word or Integer

var
StartTime, ElapsedTime: Cardinal;
procedure WatchExecute;
var
    CentiSecs,Secs,Mins: Cardinal;
Dmitry.

Re: StopWatch

DriveSoft wrote:

You should use type Cardinal instead Word or Integer

var
StartTime, ElapsedTime: Cardinal;
procedure WatchExecute;
var
    CentiSecs,Secs,Mins: Cardinal;

Thanks Dmitry, I changed the Word and Integer types to Cardinal but still get the same error message?

var
StartTime, ElapsedTime: Cardinal;
Paused: Boolean;


procedure Form1_OnShow (Sender: string; Action: string);
begin
    Form1.btnStart.Visible := True;
    Form1.btnRestart.Visible := False;
    Form1.btnReset.Visible := False;
    Form1.btnPause.Visible := False;
end;

procedure frmLarge_btnStart_OnClick (Sender: TObject; var Cancel: boolean);
begin
  Paused := False;
  StartTime := GetTickCount;
  Form1.btnPause.Visible := True;
  Form1.btnReset.Visible := False;
  Form1.btnStart.Visible := False;
  WatchExecute;
end;

procedure WatchExecute;
var
    CentiSecs,Secs,Mins: Cardinal;
Begin
    While Paused = False do
    Begin
      Sleep(10);
      application.processmessages;
      ElapsedTime := GetTickCount - StartTime;
      CentiSecs := (ElapsedTime div 10) mod 100;
      Secs := (ElapsedTime div 1000) mod 60;
      Mins := (ElapsedTime div 60000) mod 60;
      Form1.lblWatchTime.Caption := Format('%.2d', [Mins]) + ':' + Format('%.2d', [Secs]) +
                          ':' + Format('%.2d', [CentiSecs]);
    End;
End;

procedure frmLarge_btnPause_OnClick (Sender: TObject; var Cancel: boolean);
begin
  Paused := True;
  Form1.btnRestart.Visible := True;
  Form1.btnReset.Visible := True;
  Form1.btnStart.Visible := False;
  Form1.btnPause.Visible := False;
end;

procedure frmLarge_btnReset_OnClick (Sender: TObject; var Cancel: boolean);
begin
  StartTime := GetTickCount;
  Form1.lblWatchTime.Caption := '00:00:00';
  Form1.btnReset.Visible := False;
  Form1.btnRestart.Visible := False;
  Form1.btnPause.Visible := False;
  Form1.btnStart.Visible := True;
end;

procedure Form1_btnRestart_OnClick (Sender: string; var Cancel: boolean);
begin
    Paused := False;
    StartTime := GetTickCount - ElapsedTime;
    Form1.btnRestart.Visible := False;
    Form1.btnStart.Visible := False;
    Form1.btnPause.Visible := True;
    Form1.btnReset.Visible := False;
    WatchExecute;
end;
                  
procedure frmLarge_OnClose (Sender: string; Action: string);
begin
  Form1.btnPause.click;
end;                                        



begin

end.

I wonder is it something todo with my system/date time format. Perhaps script is missing something in taking different time formats into account? Just a wild guess.
I'm using win7 64bit.
Time formats: Short time HH:mm and long time HH:mm:ss


Only thing I could find on the web about this type of error is:
"The problem was caused by trying to store the HardwareID which is a DWORD (or LongInt)
into an Integer field and this one person had a HardwareID which had the high order bit set.

If the high order bit is set on the HardwareID, I make it a negative number and all is well."

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

AD1408
How long you don't restart your PC? Try to reboot.

Dmitry.

Re: StopWatch

DriveSoft wrote:

AD1408
How long you don't restart your PC? Try to reboot.


Aha... didn't think rebooting PC effects it. True I didn't reboot my PC for sometime. After rebooting error message mentioned above is gone.


Thank you very much Dmitry............

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Now we have a working StopWatch thanks to EHW and Dmitry.


Could somebody code the Settings form too please. I have no working idea about how to do it in MVD.

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Adam,
See if this is acceptable.  I did not use the other forms. I just changed the properties of the main form (Form1). Also, it is not necessary to have an adjustment for the speed so I did nothing with the timer tab. I added a Colors table for looking up colors for the text and background.

Post's attachments

Attachment icon StopWatch with Settings.zip 583.79 kb, 423 downloads since 2017-11-22 

Re: StopWatch

Hello all,


Little precision for you guys using the GetTickCount instruction.


This command counts the number of milliseconds elapsed since the computer started. As this is coded on a DWORD32 field, there is a limit that corresponds to a little less than 50 days uptime.


This might be suitable for personal use, but we have at the office, server with more than a year uptime so this function would not work, because it tends to 0 after 50 days : once you've reached the limit, stop - start =0 because the two variables are the same.


Source and explanation :
https://www.thoughtco.com/accurately-me … me-1058453


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: StopWatch

ehwagner wrote:

Adam,
See if this is acceptable.  I did not use the other forms. I just changed the properties of the main form (Form1). Also, it is not necessary to have an adjustment for the speed so I did nothing with the timer tab. I added a Colors table for looking up colors for the text and background.


Hi EHW,
Great stuff. Thank you very much............... Truly appreciated........................
I was wondering would it be possible to use Windows color dialog like in MVD instead of comboboxes for colors? Windows color dialog may be called by clicking on color panel on settings form or with button. Perhaps Dmitry can help us here...


Hi Mathias,
Thank you very much for the info and the link.....

Adam
God... please help me become the person my dog thinks I am.

21 (edited by ehwagner 2017-11-22 18:19:06)

Re: StopWatch

Mathias - Good to know. Thanks for the info.


Adam - Here you go. Just click on the color panels in the Settings form to bring up the color dialog.


Edit - Found a small error. I re-uploaded a new project.

Post's attachments

Attachment icon StopWatch with Settings Revised.zip 589.6 kb, 435 downloads since 2017-11-22 

Re: StopWatch

ehwagner wrote:

Adam - Here you go. Just click on the color panels in the Settings form to bring up the color dialog.

Excellent....
Thank you very much...............

Adam
God... please help me become the person my dog thinks I am.

Re: StopWatch

Hi EHW,


I have added hours to stopwatch but I couldn't get it increment. Always staying on hour 01 even when elapsed time over 2 hours?

Post's attachments

Attachment icon StopWatch EHW 3.zip 12.95 kb, 423 downloads since 2017-11-23 

Adam
God... please help me become the person my dog thinks I am.

24 (edited by derek 2017-11-24 00:50:54)

Re: StopWatch

Hi Adam,
I haven't actually tested it but on first glance, shouldn't line 131 in your 'watchexecute' procedure be
Hrs := (ElapsedTime  div 3600000) mod 60;     (it was previously 6000000).
Derek.

Re: StopWatch

derek wrote:

Hi Adam,
I haven't actually tested it but on first glance, shouldn't line 131 in your 'watchexecute' procedure be
Hrs := (ElapsedTime  div 3600000) mod 60;     (it was previously 6000000).
Derek.

Hi Derek,
You are right. Now it counts hours too.
Thank you very much.................

Adam
God... please help me become the person my dog thinks I am.