I was provided some code for a countdown timer that would tick upon starting. What the code failed to provide was a way to stop the clock from ticking down and a way to restart it via a command button.
Here is the code I have.
Code:
Dim StartTime As Long, Mincount As Integer
Dim Minutes As String, Seconds As String
Dim TimeA As Long, TimeB As Long
Dim GameLength As Integer ' Playing Time in Seconds
Private Sub timeClock_Timer()
' Dim lTemp As Long
' Dim lTemp2 As Long
' intCounter = intCounter - 1000
' lTemp = intCounter
' lTemp = lTemp / 1000 'convert to seconds
' lTemp2 = lTemp
' lTemp = (lTemp / 60) Mod 60 'Minutes
' lTemp2 = (lTemp2 Mod 60) 'Seconds
' If lTemp2 < 10 Then
' lblTime.Caption = lTemp & ":0" & lTemp2
' Else
' lblTime.Caption = lTemp & ":" & lTemp2
' End If
' If intCounter = 0 Then
' timeClock.Enabled = False
' MsgBox "Times up"
' End If
If (Timer - StartTime) / (Mincount + 1) > 60 Then Mincount = Mincount + 1
Seconds = Format$(60 * (Mincount + 1) - (Timer - StartTime), "00")
If Seconds = "60" Then Seconds = "00"
If GameLength - (Timer - StartTime) > 60 Then
Minutes = Format$((GameLength - (Timer - StartTime)) \ 60, "00:")
lblTime.Caption = Replace(Minutes & Seconds, "01:", "1:")
ElseIf GameLength - (Timer - StartTime) > 0 Then
lblTime.Caption = Replace(Format$(60 * (Mincount + 1) - (Timer - StartTime), "00:00.0"), "00:", "")
Else: 'MsgBox "Game Over!"
End If
End Sub
Private Sub cmdClock_Click()
If lblTime.Caption = "0:00" Then
'intCounter = 150000 '20 minutes
End If
If timeClock.Enabled = True Then
TimeA = Timer
'StartTime = StartTime - Timer
StartTime = StartTime + (TimeA - TimeB)
timeClock.Enabled = False
cmdClock.Caption = "Start Clock"
Else
timeClock.Enabled = True
cmdClock.Caption = "Stop Clock"
TimeB = Timer
End If
End Sub
Private Sub Form_Load()
GameLength = 120
StartTime = Timer
End Sub
Any help with getting the final piece to the puzzle finished would be greatly appreciated.
Okay I see what you did, and it is reflected in your project.
your clock starts and stops but does not resume after being stopped. This is being used in a sports application albeit basketball.
Not so important, but a down the line feature consideration... how to change the clock time in the event it is changed etc. Expecially since this clock does tenth second accuracy.
Okay. I added a new Long global variable called "SaveTimer". Maybe I could have used TimeA and/or TimeB but this works.
Code:
Private Sub cmdClock_Click()
Static bRestart As Boolean
If lblTime.Caption = "0:00" Then
'intCounter = 150000 '20 minutes
End If
If cmdClock.Caption = "Start Clock" Then
timeClock.Enabled = True
If Not bRestart Then
timeClock.Interval = 500
TimeA = Timer
StartTime = StartTime - Timer
StartTime = StartTime + (TimeA - TimeB)
bRestart = True
Else
StartTime = SaveTimer
End If
cmdClock.Caption = "Stop Clock"
TimeB = Timer
Else
SaveTimer = Timer
timeClock.Enabled = False
cmdClock.Caption = "Start Clock"
End If
' Also add this line at the bottom of timeClock_Timer()
SaveTimer = Timer
You are right it's not working properly. I'm going out for a couple of hours and I'll look at it when I get back unless you tell me you have it figured out by then.
Private Sub cmdClock_Click()
Static bRestart As Boolean
If lblTime.Caption = "0:00" Then
'intCounter = 150000 '20 minutes
End If
If cmdClock.Caption = "Start Clock" Then
timeClock.Enabled = True
If Not bRestart Then
timeClock.Interval = 500
StartTime = Timer
bRestart = True
Else
StartTime = StartTime + (Timer - SaveTimer)
End If
cmdClock.Caption = "Stop Clock"
Else
SaveTimer = Timer
timeClock.Enabled = False
cmdClock.Caption = "Start Clock"
End If
End Sub
Again I come to you guys with another buggish issue.
I also mentioned above that if I wanted to increase or decrease the time that it could be arranged. Martin told me add to start time. While it makes logical sense. My Timer code seems to not want to recognize it and spit out odd numbers on the seconds field. I feel that is where my code is flawed, or isn't bulletproof. It will eventually correct itself at the next minute mark, aka a if/then loop is passed?
I have it setup so that two prompt boxes are shown when you press adjust clock. One says minute and another second. negative integers number work here. I might make another one that says miliseconds if that is possible.
Any suggestions or am I just being too dang picky. Again guy's you are the BEST that's why I only visit here for vb stuff.
Steps to reproduce: Run the Project, click on Adjust Clock. Enter 0 for the Minutes and type in 25 or so seconds for the other prompt. Click Start Clock. The time does not add to the minutes. I have a feeling my math logic is flawed?
Private Sub cmdClock_Click()
Static bRestart As Boolean
If lblTime.Caption = "0:00" Then
' What to do when Clock Resets
' Prompt Asking to increase QTR & SetClock to Default
End If
If cmdClock.Caption = "Start Clock" Then
timeClock.Enabled = True
If Not bRestart Then
timeClock.Interval = 100
StartTime = Timer
bRestart = True
Else StartTime = StartTime - Modify + (Timer - SaveTimer)
End If
cmdClock.Caption = "Stop Clock"
Else
SaveTimer = Timer
timeClock.Enabled = False
cmdClock.Caption = "Start Clock"
End If
End Sub
The main problem is that you set StartTime at Form_Load, you adjust it, and then you reset StartTime with the highlighted code, overwriting the adjustment. The second problem is that bRestart is local to this sub so the If Not bRestart is always True. I think you want to use If Not Adjusted_Clock instead but when you do there is an overflow problem. I'll work on that in a while but maybe you can fix it first yourself.
Steps to reproduce: Run the Project, click on Adjust Clock. Enter 0 for the Minutes and type in 25 or so seconds for the other prompt. Click Start Clock. The time does not add to the minutes. I have a feeling my math logic is flawed?
Hi,
Not sure you still need it, but I've got a sample project for you (or anybody else strugling with timers) that has a resumable timer and resumable countdown.
Edit: Forgot to stop the countdown when it reached 0, fixed now.
HTH
Last edited by Jottum; Feb 22nd, 2008 at 08:25 AM.
Jottum™ XpVistaControls , (transparent) usercontrols for XP, Vista and 7 with W2K legacy support.
I made two more changes. The first immediately shows the adjusted time in the clock and the second is the addition of a form to do the adjustment rather than input boxes.
I believe I've fixed that problem. You didn't say anything about frmAdjust so I'll assume you are using it and noticed that you needed to click Exit twice to get out. I fixed that problem (by eliminating the button) and added code to adjust the tenths. Finally I changed frmMain so that you can't adjust the clock while it's running.