Hi, I would like the coding for timer that is in 00:00:00.
Printable View
Hi, I would like the coding for timer that is in 00:00:00.
You will have to be more explicit.Quote:
Originally posted by private1337
Hi, I would like the coding for timer that is in 00:00:00.
Do you mean "Timer" or "Time"?
If the latter, then presumably you want the time displayed down to the second.
VB Code:
TextBox1.Text = CStr(Format(Now, "hh:mm:ss"))
If you want the 24 hour clock then use HH instead of hh
I mean count up and count down timer like watch. If got alarm coding for people to set specifc time, I will be more happy about it :)
Hi,
To set a time as an alarm point you use a timer tick event to check Now.
If the user is entering a time period, one way is when you want to start the period, to set a variable to Now.AddHours (or minutes or seconds as you wish); set the timer interval to the appropriate level and check for Now>= the variable.
If the user is entering the actual alarm time then you simply use that entry for the variable value.
Coding a stopwatch is more complicated and someone else may come up with an answer before I have worked it out.
Thanks anyway..
Hi,
Experiment with the following;
VB Code:
Private Sub StartCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ZeroTime = Format(CDate("00:00:00"), "HH:mm:ss") Timer1.Enabled = True Timer1.Interval = 1000 End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ZeroTime = ZeroTime.AddSeconds(1) TextBox3.Text = (CStr(Format(ZeroTime, "hh:mm:ss"))) TextBox3.Update() End Sub
That has a bug at the moment. The counting starts at 12.00.00 instead of 00.00.00.
HI,
Cured it!!
VB Code:
Dim ZeroTime As Date ' Form scope Dim RunTime As Date Dim EndTime As Date ' depends on whether you are going to use a time or a period Dim bTimer As Boolean = False Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ZeroTime = Format(CDate("00:00:00"), "HH:mm:ss") RunTime=ZeroTime Timer1.Enabled = True Timer1.Interval = 1000 End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If bTimer = False Then RunTime = RunTime.AddSeconds(1) TextBox3.Text = (CStr(Format(RunTime, "HH:mm:ss"))) Here put code to check if EndTime has been reached or passed. End If End Sub Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click If btnPause.Text = "Pause" Then btnPause.Text = "Resume" bTimer = True Else btnPause.Text = "Pause" bTimer = False End If End Sub
If you want to count down then amend the appropriate line to
RunTime = RunTime.AddSeconds(-1)
oh... very cool!!! Thanks :)
Hi, can I have a pause/resume button? And can I check it to the format 00:00:00:00 so that I have more accurate timing.
Hi,
Use an If ... Then around the timer_tick event code, checking the state of a boolean varied by the button. Change the text of the button in accordance with the action next required.
With regard to hundredths of a second, I will have to experiment. Have a go yourself and post the result.
Oh.. Can u give me the If Then coding too cause I not really understand.. Thanks :) Wish to hear from u soon the hudnreth of second...
Hi,
I've amended my posted code to include the pause/restart facility.
I was hoping someone else had the answer to the 100's second. I will work on it.
Timers are not accurate, setting an interval of 1000 will not accurately reflect 1 second.
If I were you I'd set it to something like 50 and then use an IF to decide whether a second has passed since the last time the clock was updated, if so then add 1 second to the clock. From second This will be much more accurate.
Setting the interval to 5 should fix the 1/100th problem too.
Hi,
I cannot find any format which will display less than 1 second, so you will have to create your own custom format. That is beyond me at the moment so I would have to approach the matter differently.
Instead of using the DateTime approach, simply increment a double variable by 1 on every timer_tick event (set the interval to 10 for onehundredth) and then evaluate the variable and display it in the textbox ( first divide by 10 then by 60, then by 60 to give you the hours, minutes, seconds and 100'ths)
Have a go at coding that and post your attempt, cos me simply telling you is not helping you.
Hi, is there a standard format to display 10'ths of a second?Quote:
Originally posted by wossname
Setting the interval to 5 should fix the 1/100th problem too.
Timespan.Ticks, take a look.
Sorry, my brain seems to have gone numb. The MSDN article does not say how to use it in formatting. Also, am I reading it right when it says a Tick is one ten-millionth of a second???????Quote:
Originally posted by wossname
Timespan.Ticks, take a look.
I did look at timespan before and rejected it for minute period counting purposes but if you know different please advise.
I have incorporated your recommendation to use shorter intervals for the timer for greater accuracy -- thanks.
Private 1337.
So far I have the following code. It does the calculations OK but I am having problem with the display. It does not recognise the minutes break at 60 seconds etc. See what you can do with it.
VB Code:
Dim ZeroTime, EndTime, RunTime, TimeLeft As Double Dim sTime As String Dim bTimer As Boolean = False Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ZeroTime = 0 EndTime = 0 Timer1.Enabled = True Timer1.Interval = 1 sTime = "" End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If bTimer = False Then EndTime += 0.1 TimeLeft = EndTime + 0.001 If TimeLeft >= 1 Then sTime = CStr(Int(TimeLeft)) ' display 100'ths second RunTime = TimeLeft Mod 100 TimeLeft = Int(TimeLeft / 100) sTime = CStr(RunTime) & ":" & sTime RunTime = TimeLeft Mod 60 TimeLeft = Int(TimeLeft / 60) sTime = CStr(RunTime) & ":" & sTime RunTime = TimeLeft Mod 60 TimeLeft = Int(TimeLeft / 60) sTime = CStr(RunTime) & ":" & sTime TextBox3.Text = sTime & CStr(Int(TimeLeft)) End If End If End Sub
Hi,
Any progress?
I have solved nearly all of the problem, but the following code still has two bugs.
The seconds field does not contain a leading zero when there is only one digit.
The Hundredths field does not show up, even though the underlying calculation is taking place.
VB Code:
Dim ZeroTime, EndTime, RunTime, TimeLeft As Double Dim sTime As String Dim bTimer As Boolean = False Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ZeroTime = 0 EndTime = 23700000 Timer1.Enabled = True Timer1.Interval = 1 sTime = "" End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If bTimer = False Then sTime = "" EndTime += 1 'every 1000 th sec increase by 1. TimeLeft = EndTime + 0.001 'ensure value is rounded up Runtime = Int(TimeLeft) / 100 / 60 / 60 'calculate hours sTime = FormatRunTime(CStr(Int(Runtime))) TimeLeft = (Runtime - Int(Runtime)) * 60 + 0.0001 'calculate minutes sTime = FormatRunTime(CStr(Int(TimeLeft))) TimeLeft = (TimeLeft - Int(TimeLeft)) * 60 'calculates seconds sTime = FormatRunTime(CStr(Int(TimeLeft))) TimeLeft = Int(TimeLeft - Int(TimeLeft)) * 100 + 0.0001 'calculates 100 ths sTime = FormatRunTime(CStr(Int(TimeLeft))) TextBox3.Text = sTime End If End Sub Private Function FormatRunTime(ByVal rt As String) If Val(rt) < 1 Then rt = "00" ElseIf Val(rt) < 10 Then rt = "0" & rt End If rt = sTime & Format(Val(rt), 0) & ":" Return rt End Function Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click If btnPause.Text = "Pause" Then btnPause.Text = "Resume" bTimer = True Else btnPause.Text = "Pause" bTimer = False End If End Sub
Hi,
Cracked it! This code can be tidied up a little by using a function to calculate the time values.
VB Code:
Dim ZeroTime, EndTime, RunTime, TimeLeft As Double Dim sTime As String Dim bTimer As Boolean = False Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ZeroTime = 0 EndTime = 23712345 Timer1.Enabled = True Timer1.Interval = 1 sTime = "" End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If bTimer = False Then sTime = "" EndTime += 1 'every 1000 th sec increase by 1. TimeLeft = EndTime + 0.001 'ensure value is rounded up Runtime = Int(TimeLeft) / 100 / 60 / 60 'calculate hours sTime = FormatRunTime(CStr(Int(Runtime))) TimeLeft = (Runtime - Int(Runtime)) * 60 + 0.0001 'calculate minutes sTime = FormatRunTime(CStr(Int(TimeLeft))) TimeLeft = (TimeLeft - Int(TimeLeft)) * 60 + 0.0001 'calculates seconds sTime = FormatRunTime(CStr(Int(TimeLeft))) TimeLeft = (TimeLeft - Int(TimeLeft)) * 100 + 0.0001 'calculates 100 ths sTime = FormatRunTime(CStr(Int(TimeLeft))) TextBox3.Text = sTime End If End Sub Private Function FormatRunTime(ByVal rt As String) rt = Format(Val(rt), 0) If Val(rt) < 1 Then rt = "00" ElseIf Val(rt) < 10 Then rt = "0" & rt End If rt = sTime & rt & ":" Return rt End Function Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click If btnPause.Text = "Pause" Then btnPause.Text = "Resume" bTimer = True Else btnPause.Text = "Pause" bTimer = False End If End Sub
Now someone will come up with a way to format time in 100 ths sec:sick:
Thanks for everyone help especially taxes. Now the coding is getting more profound... But now the timer starts @ 65:52:03:00: and it seems to have the hundreth of second but start at the wrong time? Any idea??
Btw, can u modify the Pause coding
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
If btnPause.Text = "Pause" Then
btnPause.Text = "Resume"
bTimer = True
Else
btnPause.Text = "Pause"
bTimer = False
End If
End Sub
to if I never click the Start button, the Pause button can't be clicked?
I can't set to count down mode when I modify to -1. It prompt meQuote:
Originally posted by taxes
If you want to count down then amend the appropriate line to
RunTime = RunTime.AddSeconds(-1)
An unhandled exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately.
Ticks must be between DatEime.MinValue.Ticks and
Date.Time.MaxValue.Ticks.
Parameter name: ticks
when I Start the timer using the Start button. Any idea?
Ya.. that is what I want. But if it have more 1 more 00: in front that will be better. So it will be 00:00:00:00 . Is it need to change the interval again to which number?Quote:
Originally posted by wossname
Setting the interval to 5 should fix the 1/100th problem too.
I don't understand you. The code I posted DOES displayQuote:
Originally posted by private1337
Ya.. that is what I want. But if it have more 1 more 00: in front that will be better. So it will be 00:00:00:00 . Is it need to change the interval again to which number?
00:00:00:00
i.e. hours, minutes, seconds, hundredths. If you are getting a diferent result, check the code you are using.
The Timer.Interval simply sets the time between firing of the Timer_Click events. Settin the Interval to 1000 fires the event every one-thousandth of a second, to give you better accuracy as Wossname posted.
By the way, there is no point in using another function to calculate the time valuse as I mentioned, because you actually have to write more code that way.
Sorry, I set the start time to a figure which enabled me to test the hours coding and forgot to reset it when I posted:blush:Quote:
Originally posted by private1337
Thanks for everyone help especially taxes. Now the coding is getting more profound... But now the timer starts @ 65:52:03:00: and it seems to have the hundreth of second but start at the wrong time? Any idea??
Change EndTime = 23712345
to EndTime=ZeroTime
To do what you wanted to the btnPause:
In the Load event of your form put
btnPause.Enabled = False
(or you could put btnPause.Visible + False to hide it completely)
Then in the start Button click event (I called it Button4) put
btnPause.Enabled = True (or .Visible = True)
That means you will have to have a Stop button for which you will use similar Enabled code and in its click event include making itself and btnPause disabled etc.
The program WILL count backwards from a given time if you amendQuote:
Originally posted by private1337
I can't set to count down mode when I modify to -1. It prompt me
An unhandled exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately.
Ticks must be between DatEime.MinValue.Ticks and
Date.Time.MaxValue.Ticks.
Parameter name: ticks
when I Start the timer using the Start button. Any idea?
EndTime += 1
to
EndTime -= 1
I suspect that you amended the Timer.Interval
value to -1 :bigyello: But that should have given you a different error message. Check all of your code carefully with mine.
Hi, my timer seems to have something wrong. By the way, my timer will shown 00:00:00:00: if I reset it, any idea that how can I make off the last : away?
And your time seems not run with seconds, it slows down and I saw it slow down like from 00:00:00:00 to 00:00:00:99 and actual run by seconds should be 00:00:00:00 to 00:00:00:60.
I not so understand what you mean by this, can u explained more further. I want to have a TextBox for people to input the time to ring and I would like to have the Snooze function codings etc... Wish you can explained more further. Thanks :)Quote:
Originally posted by taxes
Hi,
To set a time as an alarm point you use a timer tick event to check Now.
If the user is entering a time period, one way is when you want to start the period, to set a variable to Now.AddHours (or minutes or seconds as you wish); set the timer interval to the appropriate level and check for Now>= the variable.
If the user is entering the actual alarm time then you simply use that entry for the variable value.
Coding a stopwatch is more complicated and someone else may come up with an answer before I have worked it out.
NO. My code is correct.. the right hand :00 represents hundredths of a second not 60 ths of anything.Quote:
Originally posted by private1337
Hi, my timer seems to have something wrong. By the way, my timer will shown 00:00:00:00: if I reset it, any idea that how can I make off the last : away?
And your time seems not run with seconds, it slows down and I saw it slow down like from 00:00:00:00 to 00:00:00:99 and actual run by seconds should be 00:00:00:00 to 00:00:00:60.
Sorry I had not noticed the final : amend the code as follows (Only relative code shown)
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If bTimer = False Then sTime = "" EndTime -= 1 ' every 1000 th sec increase by 1. TimeLeft = EndTime + 0.001 'ensure value is rounded up RunTime = Int(TimeLeft) / 100 / 60 / 60 'calculate hours sTime = FormatRunTime(CStr(Int(RunTime))) & ":" TimeLeft = (RunTime - Int(RunTime)) * 60 + 0.0001 'calculate minutes sTime = FormatRunTime(CStr(Int(TimeLeft))) & ":" TimeLeft = (TimeLeft - Int(TimeLeft)) * 60 + 0.0001 'calculates seconds sTime = FormatRunTime(CStr(Int(TimeLeft))) & ":" TimeLeft = (TimeLeft - Int(TimeLeft)) * 100 + 0.0001 'calculates 100 ths sTime = FormatRunTime(CStr(Int(TimeLeft))) TextBox3.Text = sTime End If End Sub Private Function FormatRunTime(ByVal rt As String) rt = Format(Val(rt), 0) If Val(rt) < 1 Then rt = "00" ElseIf Val(rt) < 10 Then rt = "0" & rt End If rt = sTime & rt Return rt End Function
Hi,
"I not so understand what you mean by this, can u explained more further. I want to have a TextBox for people to input the time to ring and I would like to have the Snooze function codings etc... Wish you can explained more further. Thanks "
You really ought to be trying to write this code yourself and then post your attempts if you need help. You are asking me to write the entire code for you. I will work on this but will not post any more code until you make a reasonable attempt.
Sorry too. I have create another new clean form and check for my error. The last 00: is fixed. But the seconds still not accurate. I try to configure and I think the BOLD coding 100 should change to 60 to be accurate. Because I try clean my codings and a new form also got problem. So I try change to 60 and it seems work for me in seconds mode.
If I change it, will it affect the program?
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If bTimer = False Then
sTime = ""
EndTime -= 1 ' every 1000 th sec increase by 1.
TimeLeft = EndTime + 0.001 'ensure value is rounded up
RunTime = Int(TimeLeft) / 100 / 60 / 60 'calculate hours
sTime = FormatRunTime(CStr(Int(RunTime))) & ":"
TimeLeft = (RunTime - Int(RunTime)) * 60 + 0.0001 'calculate minutes
sTime = FormatRunTime(CStr(Int(TimeLeft))) & ":"
TimeLeft = (TimeLeft - Int(TimeLeft)) * 60 + 0.0001 'calculates seconds
sTime = FormatRunTime(CStr(Int(TimeLeft))) & ":"
TimeLeft = (TimeLeft - Int(TimeLeft)) * 100 + 0.0001 'calculates 100 ths
sTime = FormatRunTime(CStr(Int(TimeLeft)))
TextBox3.Text = sTime
End If
End Sub
Why do you say the seconds are not accurate? I have cut and pasted your coding into my timer_tick event and it works perfectly. I have compared the seconds and minutes display the stopwatch function in my wristwatch and they are correct.
The 100 ths display is far too fast for you to read (your eyes work at approximately one- twentyfifth of a second so even if your eyesight is very sharp you will only see every fourth character of the display).
Oh.. Finally resoved the Timer Things. The Start Pause etc. button I will configure myself. Don't wish to trouble you much. But is it possible to make the countdown mode to let user enter specifc Days / Hours / Minutess / Seconds in the TextBox?
I think this is gonna hard but I don't think TextBox is the suitable choice.. Any idea in mind??
For the alarm thing, maybe I can explained in this way, the Alarm for my program will be like when the user set specifc time, it will play a specifc sound file and prompt a message box.
Things that needed I think should be a Timer of course, TextBox for user to input time but I don't think its the best, A Browse Button for user to choose a list of wav sound file to play. And probaly a messagebox or a form in AlwaysOnTop mode.
Always on Top coding anyone has it or understand it?
Hi,
I would suggest using NumericUpDown boxes to accept a target time input as this will remove most possibilities of entry error. Use 3 boxes alongside each other, one for hours, one for minutes, one for seconds. (If you are also using days then you obviously need 4 boxes)
For the Alarm sequence, you are obviously going to check for the display being <= 00:00:00:00:00, but you will have to be careful in how you do the comparison. It might be easier to check the value of each segment and set a boolean to indicate that segment reaching the relative number. e.g. Say the target time is 1 day, 5 hours, 10 minutes and 30 seconds, then as soon as the days have reached 00 set bdays to true. Then when the hours have reached 00, if bdays is true set the bHours to true etc.
Search this forum for Always on Top as it has been raised before.
Ya.. NumericUpDown is a great choice to choose. So it will be counting down on the NumericUpDown box or a new TextBox?
For the Alarm things, 2nd Paragraph, I not really sure what your are saying, but I will try your method or other method..
For the AlwaysOnTop, I can't find it in this forum. It all is in Classic Visual Basic. I need it for VB.Net... And can I have the Browse button coding?
Quote:
Originally posted by private1337
Ya.. NumericUpDown is a great choice to choose. So it will be counting down on the NumericUpDown box or a new TextBox?
For the Alarm things, 2nd Paragraph, I not really sure what your are saying, but I will try your method or other method..
For the AlwaysOnTop, I can't find it in this forum. It all is in Classic Visual Basic. I need it for VB.Net... And can I have the Browse button coding?
1. NumericUpDown is used simply to allow the user to set the time at which the alarm will sound. In this sense "Time" means the amount of days, hours, seconds, commencing now, on which the alarm will sound. So you still use a label (in preference to a textbox) to display the timer progress - the same one as you are using now.
2. Note. You will need to experiment a lot, so you will learn a lot:bigyello:
3. I will have a look at always on top.
4. With regard to browsing through sound files, you can use either an openfiledialog box, a listbox, a listviewbox or a dropdown combobox, depending on the extent of your knowledge and the code for a browse button will be very short, just enough to make the relative box avtive. You will have to be prepared to do some homework on this.
Thanks for your advice, I found browse button coding on
http://www.vbforums.com/showthread.p...=browse+button
It works great but I need to do some edit the coding though. But under the
If trim(OFD.FileName) > "" Then
'Do your upload stuff -> How can I set C:/ As Default Directory? I try OFD.InitialDirectory = ("C:/") but it can't work
End If
Waiting for your Always On Top commands and your help if needed... Thanks :)
Hi,
For always on top use Me.Topmost = True in the form_load event.
Look up OpenFile Dialog in MSDN Help.
openFileDialog1.InitialDirectory = "c:\"
You have the slash going the wrong way:bigyello:
Thanks :) The both coding works..
I want a Process to start at a Spectific time based on the Computer clock.
What should I Be Searching for? I Tried Timer, tried a Few Links and they all gotta do with what this Thread does.
Example: do a Sub at 16:55:45.
And if this Changes, I need to enter the Time in a inputbox and it must convert to Date I guess.
Do I Compare now.tolongtimestring to Something ... Completly Lost