| | |
Setting Timer/Clock Precision
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Hey there!
I'm making a break timing device for work (so I can be more aware of how long I'm taking on my breaks. I'm bad at keeping track.) If someone could help me out, it would be greatly appreciated.
What the program does:
- After I click "break" it Counts down from 15 minutes.
- Once the timer hits zero, it counts back up (in red text)
What I Need:
- As of right now, it just tells me how many minutes are left. I need to find out if/how I can display it by the second. (example: 15:00, 14:59, 14:58, 14:57, etc. right down to 0:00. Instead of just "15 Minutes, 14 minutes, etc) How would I do this??
This is the code I have so far:
Option Explicit
Dim StartTime As Date
Dim BreakTime As Long
Private Sub cmdBreak_Click()
cmdbreak.Enabled = False
cmdImBack.Enabled = True
StartTime = Now()
BreakTime = 1
Timer1.Enabled = False
Timer1.Interval = 5000
Timer1.Enabled = True
Timer1_Timer
End Sub
Private Sub cmdImBack_Click()
cmdImBack.Enabled = False
Timer1.Enabled = False
cmdbreak.Enabled = True
Text1.Text = ""
End Sub
Private Sub Form_Load()
cmdbreak.Enabled = True
cmdImBack.Enabled = False
Text1.Text = ""
End Sub
Private Sub Timer1_Timer()
Dim MinutesGone As Long
MinutesGone = DateDiff("n", StartTime, Now())
If MinutesGone < BreakTime Then
With Text1
.Text = (BreakTime - MinutesGone) & _
" mins"
.ForeColor = vbBlack
End With
Else
With Text1
.Text = "-" & (MinutesGone - BreakTime)
.ForeColor = vbRed
End With
End If
End Sub
I'm making a break timing device for work (so I can be more aware of how long I'm taking on my breaks. I'm bad at keeping track.) If someone could help me out, it would be greatly appreciated.
What the program does:
- After I click "break" it Counts down from 15 minutes.
- Once the timer hits zero, it counts back up (in red text)
What I Need:
- As of right now, it just tells me how many minutes are left. I need to find out if/how I can display it by the second. (example: 15:00, 14:59, 14:58, 14:57, etc. right down to 0:00. Instead of just "15 Minutes, 14 minutes, etc) How would I do this??
This is the code I have so far:
Option Explicit
Dim StartTime As Date
Dim BreakTime As Long
Private Sub cmdBreak_Click()
cmdbreak.Enabled = False
cmdImBack.Enabled = True
StartTime = Now()
BreakTime = 1
Timer1.Enabled = False
Timer1.Interval = 5000
Timer1.Enabled = True
Timer1_Timer
End Sub
Private Sub cmdImBack_Click()
cmdImBack.Enabled = False
Timer1.Enabled = False
cmdbreak.Enabled = True
Text1.Text = ""
End Sub
Private Sub Form_Load()
cmdbreak.Enabled = True
cmdImBack.Enabled = False
Text1.Text = ""
End Sub
Private Sub Timer1_Timer()
Dim MinutesGone As Long
MinutesGone = DateDiff("n", StartTime, Now())
If MinutesGone < BreakTime Then
With Text1
.Text = (BreakTime - MinutesGone) & _
" mins"
.ForeColor = vbBlack
End With
Else
With Text1
.Text = "-" & (MinutesGone - BreakTime)
.ForeColor = vbRed
End With
End If
End Sub
what you do, is set the timer's interval to 1 second. Then use a variable to keep track of the minutes (start at 0, and add 1 to the variable every time the timer fires, then, if the variable is equal to 59 [0-59 is 60 seconds], then reset the variable back to 0, and add 1 to your "minute" count). This will let you keep track of the program down to the seconds, and will still keep the integrity of the minutes... if you need more in-depth help, attach the project in a .zip file, and I'll gladly fiddle with your code.
hey Coma, long time no see! how are you.
and about code, I have change some of your code, i make it simple as possible and work very fine too.
and about code, I have change some of your code, i make it simple as possible and work very fine too.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Option Explicit Dim SecLeft As Integer Dim MinLeft As Integer Private Sub cmdBreak_Click() cmdbreak.Enabled = False ' To stop them resetting ! cmdImBack.Enabled = True MinLeft = 15 SecLeft = 0 Timer1.Enabled = True Timer1_Timer ' to draw it once! End Sub Private Sub cmdImBack_Click() cmdImBack.Enabled = False Timer1.Enabled = False cmdbreak.Enabled = True Text1.Text = "--" End Sub Private Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 1000 cmdbreak.Enabled = True cmdImBack.Enabled = False Text1.Text = "--" End Sub Private Sub Timer1_Timer() If SecLeft = 0 Then SecLeft = 60 MinLeft = MinLeft - 1 If MinLeft = 0 Then Timer1.Enabled = False End If SecLeft = SecLeft - 1 Text1.Text = MinLeft & " : " & SecLeft End Sub
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
Hey!
Ahh, thanks for you help but it seems to be freezing at the 1 minute mark, it's breaking the double digit seconds format (ex: 1:8 instead of 1:08), and its not turning red, or counting back up after it hits zero.
I'm totally lost guys
Any help/tinkering would be greatly appreciated coma. Sorry for being a bother.
Take care,
-Tony
Ahh, thanks for you help but it seems to be freezing at the 1 minute mark, it's breaking the double digit seconds format (ex: 1:8 instead of 1:08), and its not turning red, or counting back up after it hits zero.
I'm totally lost guys

Any help/tinkering would be greatly appreciated coma. Sorry for being a bother.
Take care,
-Tony
try this one
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Option Explicit Dim SecLeft As Integer Dim MinLeft As Integer Private Sub cmdBreak_Click() cmdbreak.Enabled = False ' To stop them resetting ! cmdImBack.Enabled = True MinLeft = 15 SecLeft = 0 Timer1.Enabled = True Timer1_Timer ' to draw it once! End Sub Private Sub cmdImBack_Click() cmdImBack.Enabled = False Timer1.Enabled = False cmdbreak.Enabled = True Text1.Text = "--" End Sub Private Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 1000 cmdbreak.Enabled = True cmdImBack.Enabled = False Text1.Text = "--" End Sub Private Sub Timer1_Timer() If SecLeft = 0 Then SecLeft = 60 If MinLeft = 0 Then Timer1.Enabled = False Exit Sub End If MinLeft = MinLeft - 1 End If SecLeft = SecLeft - 1 Text1.Text = MinLeft & " : " & Right("0" & SecLeft, 2) End Sub
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: sql query updating problem
- Next Thread: Problem formating VB string for clipboard
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column 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 retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows








)