[Gambas-user] Timer backwards?

Bruce Steers bsteers4 at gmail.com
Fri Mar 5 17:15:31 CET 2021


On Fri, 5 Mar 2021 at 09:43, Rolf-Werner Eilert <rwe-sse at osnanet.de> wrote:

> Just stumbled over this one - there isn't a property like
> Timer.Backwards or Timer.StopWatch, right?
>
> Might be a practical thing to have:
>
> Timer.Backwards as Boolean (to set stopwatch mode)
> Timer.Start as Integer (in milliseconds to set the start value)
> Timer_Stop - event to trigger some action on counter 0
>
> Setting Timer.Start > 0 could start the Timer from code.
>
> Regards
> Rolf
>

Well for starters Timer.Start is already a function so cannot use that name.
And as vogon commander bruce said "count down or up, what's the difference
really?"

But if you must have it then to implement this yourself would take very
little code...
I'll show you, it's as easy as this......

Create a new class file in your app and Call it StopWatch.class

copy and paste this text into it...
-----------cut------------------------------------
' Gambas class file

Export

Inherits Timer

Event ZeroReached
Public Const _Properties As String = "*,-Delay"  ' hide Delay as we only
want seconds

Property CountDown As Integer Use $iCountDown

Private $Timer As Timer

Public Sub _new()
  $Timer = New Timer As "Time"
  $Timer.Delay = 1000
End


Public Sub BeginCountDown(Seconds As Integer)
  $iCountDown = Seconds
  $Timer.Start
End

Public Sub Time_Timer()
  Dec $iCountDown
  If $iCountDown > 0 Then Return
  $Timer.Stop
  Raise ZeroReached
End
------cut--------------

So this class inherits Timer and adds the Property CountDown , a command
BeginCountDown(seconds) and an event ZeroReached.
It will be in the form designer with the normal timer on the "Special"
family tab and can be instanced as you would a normal Timer.
So now your app has a countdown Timer and could use code like this...
'' Begin a 10 second countdown
Public Sub Button1_Click()
  StopWatch1.BeginCountDown(10)
End

'' react to countdown reaching zero
Public Sub StopWatch1_ZeroReached()
  Message("countdown reached zero")
End

'' reading StopWatch1.CountDown tells us how many seconds to go.
Public Sub Button2_Click()
  Last.Text = "How long to go " & StopWatch1.CountDown & " seconds"
End

Wishing Well
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210305/72c98a29/attachment.htm>


More information about the User mailing list