|
-
Jan 5th, 2008, 10:17 AM
#1
Thread Starter
Member
[RESOLVED] Countdown Timer with minutes seconds and milliseconds
hi everybody!
i have to make a basketball scoreboard application in vb 6.
i pretty much know how to do everything except for the countdown timer which has to count down all the way from 8 minutes to 0. it would also be nice if it makes a beep when it reaches 0. so here's the problem. how to make a timer with minutes seconds n milliseconds?? it should look like this
minutes:seconds:milliseconds
someone please help!! im desperate
thanks in advance
-
Jan 5th, 2008, 10:47 AM
#2
Hyperactive Member
Re: Countdown Timer with minutes seconds and milliseconds
you could put a timer control on the form, and set its interval to 1000.
then do what's next
Last edited by Kaimonington; Jan 5th, 2008 at 10:50 AM.
-
Jan 5th, 2008, 10:49 AM
#3
Hyperactive Member
Re: Countdown Timer with minutes seconds and milliseconds
make a variable called time as single, and set it to 8.
double click the timer, and type in this
Code:
time = time - 1
if time = 0 then MsgBox "done", vbOKOnly + vbInformation, "done"
text1.text = str(time)
-
Jan 5th, 2008, 10:51 AM
#4
Hyperactive Member
Re: Countdown Timer with minutes seconds and milliseconds
I dont know about the beep, miliseconds are a bit dodgey because I tried that on my computer and it takes slightly longer than a milisecond because the timer has to do stuff aswell.
-
Jan 5th, 2008, 11:09 AM
#5
Re: Countdown Timer with minutes seconds and milliseconds
Add Label control,Command button and Timer to your form first, then copy/paste and run this sample code:
Code:
Option Explicit
Dim lSeconds As Long
Private Sub Form_Load()
lSeconds = 8 * 60000
Label1.Caption = "00:80:00.000"
Timer1.Interval = 1 '1/1000 of a sec
Timer1.Enabled = False
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Timer()
Dim timeleft As String
lSeconds = lSeconds - 1
timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
Format((lSeconds Mod 60000) / 1000, "##.000")
Label1.Caption = timeleft
If lSeconds = 0 Then Timer1.Enabled = False
End Sub
-
Jan 5th, 2008, 11:26 AM
#6
Thread Starter
Member
-
Jan 5th, 2008, 11:33 AM
#7
Hyperactive Member
Re: Countdown Timer with minutes seconds and milliseconds
ok, this code is alot longer than mine and has miliseconds, but when I do this, It doesnt do it every millisecond, it does it slightly more than a milisecond, I think because it needs time to change the textbox's text, but the seconds are more or less the same.
-
Jan 5th, 2008, 11:43 AM
#8
Re: Countdown Timer with minutes seconds and milliseconds
If you need TWO decimals then change this "##.000" to "##.00" - however MILLI really means 1/1000th.
-
Jan 5th, 2008, 11:44 AM
#9
Re: Countdown Timer with minutes seconds and milliseconds
 Originally Posted by Kaimonington
ok, this code is alot longer than mine and has miliseconds, but when I do this, It doesnt do it every millisecond, it does it slightly more than a milisecond, I think because it needs time to change the textbox's text, but the seconds are more or less the same.
What are you talking about?
-
Jan 5th, 2008, 11:52 AM
#10
Frenzied Member
Re: Countdown Timer with minutes seconds and milliseconds
Final:
Code:
Option Explicit
Dim lSeconds As Long
Private Sub Form_Load()
lSeconds = 8 * 60000
Label1.Caption = "00:08:00.00"
Timer1.Enabled = False
Timer1.Interval = 1 '1/1000 of a sec
Me.Caption = "StopWatch!"
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then Command1.Caption = "Stop"
If Timer1.Enabled = False Then Command1.Caption = "Start"
End Sub
Private Sub Timer1_Timer()
Dim timeleft As String
lSeconds = lSeconds - 1
timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
Format((lSeconds Mod 60000) / 1000, "##.00")
Label1.Caption = timeleft
If lSeconds = 0 Then Timer1.Enabled = False
End Sub
-
Jan 5th, 2008, 12:06 PM
#11
Re: Countdown Timer with minutes seconds and milliseconds
 Originally Posted by Zach_VB6
Final:
Says Who? And what's the point of copying somebody else's code entirely?!
-
Jan 5th, 2008, 01:03 PM
#12
Frenzied Member
Re: Countdown Timer with minutes seconds and milliseconds
If you only need 2 places for the fraction of a second part then I recommend setting the timer interval to 10. That way that timer has more time to run the code and keep a more accurate time.
-
Jan 5th, 2008, 01:44 PM
#13
Re: Countdown Timer with minutes seconds and milliseconds
A timer can't really run any faster than ~15 milliseconds. Set the interval to anything less and it runs at ~15 milliseconds. It's also not very accurate, the following is the results (in milliseconds) of timing a timer of interval 1 with QueryPerformanceCounter.
15.65
21.4
9.77
15.66
17.67
15.6
15.63
14.64
23.39
9.82
and interval 15...
15.08
3.91
18.6
15.63
15.55
15.77
13.61
23.47
12.66
9.85
-
Jan 5th, 2008, 03:30 PM
#14
Re: Countdown Timer with minutes seconds and milliseconds
Here's my effort, might need tweaking but should run much more accurately
Code:
Option Explicit
Dim EndTime As Single
Private Sub Command1_Click()
EndTime = (Timer + 8 * 60) Mod 86400
Timer1.Interval = 20
End Sub
Private Sub Timer1_Timer()
Dim CtDn As Long
CtDn = (EndTime - Timer) * 100
If CtDn < 0 Then
If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
CtDn = CtDn + 8640000
Else
Label1.Caption = "0:00:00"
Beep 'or somesuch
Timer1.Interval = 0
Exit Sub
End If
End If
Label1.Caption = Format((CtDn \ 6000) Mod 60, "0:") & Format((CtDn \ 100) Mod 60, "00:") & Format(CtDn Mod 100, "00")
End Sub
I'm assuming hundredths of seconds in preference to milliseconds
-
Jan 5th, 2008, 04:03 PM
#15
Re: Countdown Timer with minutes seconds and milliseconds
Milk is correct. The system Time is updated every 1/60 second (~ 16.67 milliseconds). So don't base on counter to display realtime.
Code:
Option Explicit
Dim Finish As Single
Private Sub Command1_Click()
Finish = Timer + 8 * 60 '-- 8 minutes from now (in seconds)
Label1.Caption = "8:00.00"
Timer1.Interval = 10
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim CountDown As Single
Dim s As Long
CountDown = Finish - Timer '-- in seconds (with decimal part)
If CountDown <= 0 Then
Label1.Caption = "0:00.00"
Timer1.Enabled = False
Else
s = Int(CountDown)
Label1.Caption = (s \ 60) & ":" & Format(s Mod 60, "00") & _
Format(CountDown - s, ".00")
End If
DoEvents
End Sub
PS.: Need to be considered if Timer pass midnight.
Last edited by anhn; Jan 6th, 2008 at 05:05 AM.
-
Jan 5th, 2008, 04:12 PM
#16
Frenzied Member
Re: Countdown Timer with minutes seconds and milliseconds
anhn - Format(Counddown - s, ".00")
Should be Format(Countdown - s, ".00") (typo )
-
Jan 5th, 2008, 04:48 PM
#17
Re: Countdown Timer with minutes seconds and milliseconds
Have a look at my code for a crossing midnight tweak...
-
Jan 5th, 2008, 09:48 PM
#18
Thread Starter
Member
Re: Countdown Timer with minutes seconds and milliseconds
hey thanks guys!
you really helped a lot. i took some advice from all of you and mixed up a little and got a nice working timer!
thanks!
-
Jan 5th, 2008, 11:58 PM
#19
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
@ Milk, when posting Post#15, I didn't see your post (even after 33 minutes).
They look very similar.
-
Jan 6th, 2008, 01:48 AM
#20
Thread Starter
Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
hi
i already asked this to milk but i am getting no reply...
with his code i can stop the timer, but when i click on the command again it restart from 8 minutes. do you know how to make it continue from where it stopped?
thanks i really need this...
-
Jan 6th, 2008, 02:21 AM
#21
Frenzied Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
Make 2 commandbuttons (on top of each other, same size)
Code:
Option Explicit
Dim EndTime As Single
Dim Index As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.Visible = False
Command2.Visible = True
Index = Index + 1
If Index = 1 Then EndTime = (Timer + 8 * 60) Mod 86400
Timer1.Interval = 20
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Command2.Visible = False
Command1.Visible = True
End Sub
Private Sub Form_Load()
Command1.Caption = "Start"
Command2.Caption = "Stop"
Command2.Visible = False
End Sub
Private Sub Timer1_Timer()
Dim CtDn As Long
CtDn = (EndTime - Timer) * 100
If CtDn < 0 Then
If CtDn < -1000 Then 'bit of faff to avoid over midnight countdown
CtDn = CtDn + 8640000
Else
Label1.Caption = "0:00:00"
Beep 'or somesuch
Timer1.Interval = 0
Exit Sub
End If
End If
Label1.Caption = Format((CtDn \ 6000) Mod 60, "0:") & Format((CtDn \ 100) Mod 60, "00:") & Format(CtDn Mod 100, "00")
End Sub
-
Jan 6th, 2008, 03:09 AM
#22
Thread Starter
Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
thanks a lot!
-
Jan 6th, 2008, 03:11 AM
#23
Frenzied Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
You betcha!
Let me know if you need any more assistance.
-
Jan 6th, 2008, 07:26 AM
#24
Re: Countdown Timer with minutes seconds and milliseconds
 Originally Posted by RhinoBull
Add Label control,Command button and Timer to your form first, then copy/paste and run this sample code:
Code:
Option Explicit
Dim lSeconds As Long
Private Sub Form_Load()
lSeconds = 8 * 60000
Label1.Caption = "00:80:00.000"
Timer1.Interval = 1 '1/1000 of a sec
Timer1.Enabled = False
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Timer()
Dim timeleft As String
lSeconds = lSeconds - 1
timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
Format((lSeconds Mod 60000) / 1000, "##.000")
Label1.Caption = timeleft
If lSeconds = 0 Then Timer1.Enabled = False
End Sub
 Originally Posted by Zach_VB6
Final:
Code:
Option Explicit
Dim lSeconds As Long
Private Sub Form_Load()
lSeconds = 8 * 60000
Label1.Caption = "00:08:00.00"
Timer1.Enabled = False
Timer1.Interval = 1 '1/1000 of a sec
Me.Caption = "StopWatch!"
End Sub
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled = True Then Command1.Caption = "Stop"
If Timer1.Enabled = False Then Command1.Caption = "Start"
End Sub
Private Sub Timer1_Timer()
Dim timeleft As String
lSeconds = lSeconds - 1
timeleft = Format((lSeconds \ 3600000), "00") & ":" & _
Format((lSeconds Mod 3600000) \ 60000, "00") & ":" & _
Format((lSeconds Mod 60000) / 1000, "##.00")
Label1.Caption = timeleft
If lSeconds = 0 Then Timer1.Enabled = False
End Sub
 Originally Posted by Zach_VB6
You betcha!
Let me know if you need any more assistance.
Let me know if Zach_VB6 and RhinoBull are the same person?!?!?!
What's the point? I don't understand!
@theonlyhugeg, you should not send PMs to Milk and me. Zach_VB6 is the best expert who can help you to do anything you want.
-
Jan 6th, 2008, 07:30 AM
#25
Thread Starter
Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
alright i'll keep it in mind in the future.
i have accomplished my task however and i dont think i'll be asking anymore questions about countdown timers.
thanks for your help
and i dont know if Zach_VB6 and RhinoBull are the same person...
-
Jan 6th, 2008, 11:32 AM
#26
Re: Countdown Timer with minutes seconds and milliseconds
 Originally Posted by anhn
Let me know if Zach_VB6 and RhinoBull are the same person?!?!?!
What's the point? I don't understand!
No we are not!
 Originally Posted by anhn
... Zach_VB6 is the best expert who can help you to do anything you want.
With all due respect but I'm afraid not.
-
Jan 6th, 2008, 11:36 AM
#27
Frenzied Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
I agree totally.
I am not an "expert".
-
Sep 3rd, 2015, 11:02 AM
#28
Hyperactive Member
Re: [RESOLVED] Countdown Timer with minutes seconds and milliseconds
How to make normal stopwatch timer from this example, not running backwards?
HH:mm:ss:sss
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|