| | |
Countdown Timer with minutes seconds and milliseconds!
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
hi everybody!
im in a basketball team n my coach asked me to make a scoreboard program for the huge screen we have (he knows im the only one from the team that does some programming)
im using Visual Basic 2008.
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. i know how to make a simple timer with seconds. but how to put minutes n milliseconds?? it should look like this
minutes:seconds:milliseconds
someone please help!! im desperate
thanks in advance
im in a basketball team n my coach asked me to make a scoreboard program for the huge screen we have (he knows im the only one from the team that does some programming)
im using Visual Basic 2008.
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. i know how to make a simple timer with seconds. but how to put minutes n milliseconds?? it should look like this
minutes:seconds:milliseconds
someone please help!! im desperate
thanks in advance
Last edited by theonlyhugeg; Jan 4th, 2008 at 1:18 pm.
•
•
•
•
i know how to make a simple timer with seconds. but how to put minutes n milliseconds?? it should look like this
minutes:seconds:milliseconds
Even if you convert the Now to a double precision value (which you'll have to use obtain milleseconds), the best you will get is still a change in the visible value every second.
So what you need to use is the Windows API 'QueryPerformanceCounter' in your program to obtain a higher precision of accuracy. To find how much precision your CPU will provide you use the 'QueryPerformanceFrequency' API
The degree of precision depends on your CPU and how simple you can keep your code.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Type LARGE_INTEGER LowPart As Long HighPart As Long End Type Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
I've given you a start. But here's the pseudo logic you'll need:
Determine your CPU's capabilities with the QueryPerformanceFrequency
Create a loop to provide a value to use for your millisecond display.
And you need to provide some method to get yourself out of the loop.
I'll leave the rest to you. But that should get you started.
Hank
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
This API function Return a time in miliSecond for each time called.
you can use it.
•
•
•
•
This API function Return a time in miliSecond for each time called.
you can use it.
That could be anywhere from 9 to 100 milliseconds.
If the user requires a resolution of 1 millisecond, then you'll have to use the other API's to determine if the running CPU has that capability: hence the necessity of using the QueryPerformanceFrequency and QueryPerformanceCounter APIs.
This code, when the timer property for interval is set at 1 millisecond, should give you resolution of the user's system timer.
This came out to be about 15 milliseconds on a 2.4 GHz pentium4
This came out to be about 15 milliseconds on a 2.4 GHz pentium4
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Dim blnStart As Boolean Private Sub Command1_Click() Timer1.Enabled = Not Timer1.Enabled End Sub Private Sub Form_Load() On Error GoTo Load_ERROR blnStart = True Exit Sub Load_ERROR: MsgBox Err.Description, vbInformation, "eRROR" End Sub Private Sub Timer1_Timer() Static CurrentTickCount As Long Dim lngTickCount As Long, lngDifference As Long If blnStart = True Then lngTickCount = GetTickCount CurrentTickCount = 0 blnStart = False Else lngTickCount = GetTickCount End If If CurrentTickCount = lngTickCount Then CurrentTickCount = lngTickCount lngTickCount = 0 Else Label1.Caption = lngTickCount lngDifference = lngTickCount - CurrentTickCount Debug.Print lngDifference CurrentTickCount = lngTickCount End If End Sub
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
hi
i noticed now that this forum is for vb 6... but thats no problem. i just downloaded n installed that and your code works fine! thanks! one question though... now the count is going up...like its adding time and its starting from a number like 1774796... how can i make it actually count down and start from 8 minutes??
thanks a lot
appreciate your help
i noticed now that this forum is for vb 6... but thats no problem. i just downloaded n installed that and your code works fine! thanks! one question though... now the count is going up...like its adding time and its starting from a number like 1774796... how can i make it actually count down and start from 8 minutes??
thanks a lot
appreciate your help
•
•
•
•
its starting from a number like 1774796
•
•
•
•
how can i make it actually count down and start from 8 minutes??
Everything else is just algebra and logic.
You need to have one established reference point: That's the value you derive from using the above API's and finding the current time using the Now function.
Lets' call that value lngValueA. Save that value and use it as a reference point for your minutes:seconds:milleseconds.
Once you have established that reference point. You need to come up with a formula to calculate How many minutes, how many seconds, and how many fractional seconds.
This is just basic algebra.
You need to have one established reference point: That's the value you derive from using the above API's and finding the current time using the Now function.
Lets' call that value lngValueA. Save that value and use it as a reference point for your minutes:seconds:milleseconds.
Once you have established that reference point. You need to come up with a formula to calculate How many minutes, how many seconds, and how many fractional seconds.
This is just basic algebra.
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
hi
i figured out how to do it. i used a different method... i dont think its as accurate as yours, but its not really important... in basketball the milliseconds are just there to look cool lol.
anyway thanks for your help
im using this code right now
Option Explicit
Dim EndTime As Single
Private Sub Form_Load()
Timer1.Enabled = False
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"
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 figured out how to do it. i used a different method... i dont think its as accurate as yours, but its not really important... in basketball the milliseconds are just there to look cool lol.
anyway thanks for your help
im using this code right now
Option Explicit
Dim EndTime As Single
Private Sub Form_Load()
Timer1.Enabled = False
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"
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
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Reading From Text Files into insert into Grid
- Next Thread: email and website validation
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





