Stopwatch
February 27th, 2007When learning a programming language, a stopwatch program is a common example. So here's a sample Stopwatch program created with VB Express. The program makes use of a Timer control, a label, and a button. This is very similar to how it used to be done with Visual Basic 6. Another way to do it in VB 2005 is to use the StopWatch class which is available in the .NET 2.0 Framework Class Library. But I'll save that for a future post. For now, here's the screen shot and code snip.

Dim dtStartTime As DateTime = Now
Dim tsElapsedTime As TimeSpan
Dim a As Stopwatch
Private Sub cmdStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartStop.Click
If Not Timer1.Enabled Then dtStartTime = Now()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
tsElapsedTime = Now.Subtract(dtStartTime)
With tsElapsedTime
lblElapsed.Text = .Minutes.ToString & ":" & .Seconds.ToString & "." & .Milliseconds
End With
End Sub
End Class
Download the sample project here .
Posted in Visual Basic | No CommentsNo Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
(C) by Virgilio Adriano. All rights reserved. Powered by WordPress.
Entries and comments feeds.
It took 0.414 seconds to load this page.