Add pauze function to Chronometer

Add pauze function to Chronometer

Postby Marc Venken » Tue Mar 07, 2023 10:54 pm

Mr. Rao posted a chronometer on the forum :

I'm not able to change the code so that when you hit START and then STOP, the program should continue with the value that is on the digit.
Hitting CLEAR is correct : it goes to zero
Hitting START also goes to zero and then start. This should go on ...

Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   local nSecsLapsed

   nSecsLapsed := ChronoMeter()

   ? nSecsLapsed, SECTOTIME( nSecsLapsed, .t. )

return nil

function ChronoMeter()

   local oDlg, oSay, oBtnStart, oBtnStop, oTimer, oFont, oBold
   local nStartSec, nSecsLapsed := 0

   DEFINE FONT oFont NAME "Segoe UI" SIZE 0,-16
   DEFINE FONT oBold NAME "Segoe UI" SIZE 0,-32

   DEFINE DIALOG oDlg SIZE 500,120 PIXEL TRUEPIXEL FONT oFont

   @ 40, 40 BTNBMP oBtnStart PROMPT "START" SIZE 100,40 PIXEL OF oDlg FLAT ;
      WHEN nStartSec == nil ;
      ACTION ( nStartSec := SECONDS(), oBtnStop:SetFocus() )

   @ 40,170 SAY oSay PROMPT { || SECTOTIME( nSecsLapsed, .t. ) } ;
      SIZE 160,40 PIXEL OF oDlg FONT oBold CENTER VCENTER UPDATE ;
      COLOR CLR_HGREEN, CLR_BLACK

   @ 40,360 BTNBMP oBtnStop PROMPT { || If( nStartSec == nil .and. nSecsLapsed > 0, "CLEAR", "STOP" ) } ;
      SIZE 100,40 PIXEL OF oDlg FLAT UPDATE ;
      WHEN nStartSec != nil .or. nSecsLapsed > 0 ACTION ( ;
      If( nStartSec == nil, ;
      If( nSecsLapsed > 0, nSecsLapsed := 0, nil ), ;
      ( nSecsLapsed := SecsLapsed( nStartSec ), nStartSec := nil ) ), ;
      oDlg:Update(), oBtnStart:SetFocus() )

   oDlg:bInit := <||
      DEFINE TIMER oTimer OF oDlg INTERVAL 100 ;
         ACTION If( nStartSec == nil,,( nSecsLapsed := SecsLapsed( nStartSec ),oSay:Refresh() ) )
      ACTIVATE TIMER oTimer
      return nil
      >

   ACTIVATE DIALOG oDlg CENTERED ;
      VALID ( oTimer:End(), .t. )

   RELEASE FONT oFont, oBold

return nSecsLapsed

static function SecsLapsed( nStart )

   local nLapsed := 0

   if nStart != nil
      nLapsed  := SECONDS() - nStart
      if nLapsed < 0
         nLapsed   += 86400.0
      endif
   endif

return nLapsed
 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1343
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Add pauze function to Chronometer

Postby Enrico Maria Giordano » Wed Mar 08, 2023 10:05 am

Change this:

Code: Select all  Expand view
ACTION ( nStartSec := SECONDS(), oBtnStop:SetFocus() )


with this:

Code: Select all  Expand view
ACTION ( nStartSec := SECONDS() - nSecsLapsed, oBtnStop:SetFocus() )
User avatar
Enrico Maria Giordano
 
Posts: 8328
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: nageswaragunupudi and 8 guests