|
-
Jul 19th, 2006, 07:19 PM
#1
Thread Starter
New Member
VB Timer
Hey, ive got this VB project and im trying to put in a timer.
So when u click a button a timer will start from zero and count up by 1 second. When the button is clicked again the timer will stop and a msg box will display how many seconds and milliseconds it took.
I also understand that you need a label for the time to be displayed in.
If anyone can help me out with this thatd be great, cheers
My VB Code = Very Basic Code
-
Jul 19th, 2006, 07:45 PM
#2
Re: VB Timer
It would be appreciated if you could specify which version of VB you're using in future. If it's VB 2005 then you already have a Stopwatch class available to you. If its earlier then I have posted a Stopwatch class in the CodeBank. You don't actually need a Timer to calculate elapsed time at all. If you want to display the elapsed time as it increases then you would need a Timer for that. Both the .NET 2.0 Stopwatch class and my Stopwatch class have Elapsed properties. If you add a Timer to your form with an Interval of 1000 (milliseconds), you can then call Start on the Timer and the Stopwatch at the same time. In the Timer's Tick event handler you would then do something like this:
VB Code:
Public Class Form1
Dim myStopwatch As New Stopwatch
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Start()
myStopwatch.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
Me.Label1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}", _
Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
elapsedTime.Minutes, _
elapsedTime.Seconds)
End Sub
End Class
The time displayed will not be perfect though. Because of the nature of Windows applications and the Timer class there will be times where the same time will be displayed for two seconds and then skip a second. You will get a slightly more accurate result with this:
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsedTime As TimeSpan = Me.myStopwatch.Elapsed
Me.Label1.Text = String.Format("{0:d2}:{1:d2}:{2:d2}", _
Convert.ToInt32(Math.Floor(elapsedTime.TotalHours)), _
elapsedTime.Minutes, _
elapsedTime.Seconds + Convert.ToInt32(Math.Round(elapsedTime.Milliseconds / 1000)))
End Sub
-
Jul 20th, 2006, 12:48 AM
#3
New Member
Re: VB Timer
Hey, yeah i had this same problem to, and this code really helped,
however is it possible to have just seconds and milliseconds, and be able to view the milliseconds counting upwards as well.
I use visual basic express, so any help would be cool
-
Jul 20th, 2006, 01:09 AM
#4
Re: VB Timer
 Originally Posted by Timmy_J
Hey, yeah i had this same problem to, and this code really helped,
however is it possible to have just seconds and milliseconds, and be able to view the milliseconds counting upwards as well.
I use visual basic express, so any help would be cool
You're unlikely to get a very acceptable result if you want to update a Label every millisecond because there will almost certainly be pauses in it, but you can give it a go. The principle is exactly the same. You just have to change the Timer Interval and the way you format the output string.
-
Jul 20th, 2006, 02:21 AM
#5
New Member
Re: VB Timer
okay, yeah i tried but it didnt work out, lol
thanks for your help anyway.
It has really helped out in my program
My software never has bugs. It just develops random features.
-
Jul 20th, 2006, 02:30 AM
#6
Re: VB Timer
 Originally Posted by Timmy_J
okay, yeah i tried but it didnt work out, lol
thanks for your help anyway.
It has really helped out in my program
Actually I just tried it myself and it worked better than I expected. You couldn't hope to actually read the display as it's running anyway. If you were perfroming some other processor intensive tasks you may find that it became jerky but I guess we are used to that anyway. The new format would look like this:
VB Code:
Me.Label1.Text = String.Format("{0}.{1:d3}", _
Convert.ToInt32(Math.Floor(elapsedTime.TotalSeconds)), _
elapsedTime.Milliseconds)
Last edited by jmcilhinney; Jul 20th, 2006 at 02:33 AM.
Reason: Fixed format of seconds
-
Jul 20th, 2006, 06:21 AM
#7
New Member
Re: VB Timer
Okay yeah, thats seems to work, thanks heaps
My software never has bugs. It just develops random features.
-
Jul 20th, 2006, 06:45 AM
#8
Frenzied Member
Re: VB Timer
this looks like a homework assignment
-
Jul 20th, 2006, 06:51 AM
#9
New Member
Re: VB Timer
lol yeah it sorta is, but this is my first time ive used visual basic and actually had to make a prgram, lol so im a bit stumped lol
My software never has bugs. It just develops random features.
-
Jul 20th, 2006, 04:14 PM
#10
Re: VB Timer
 Originally Posted by maged
this looks like a homework assignment 
Good point. Broke my own rules.
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
|