Calculate Seconds Between Two Events

vijay s

New member
Joined
Jul 16, 2009
Messages
3
Programming Experience
Beginner
Hi
I'm Newbie to Vb.net ... i need a help in calculating interval between two Events in Seconds... i searched net and used some, producing error.. please guide me to do this...
 
Time Elapsed in total number of seconds:

Using the stopwatch is easy. The tricky part is making it display the total number of seconds instead of the default minutes and seconds. Here is the complete code. It uses 3 buttons, a label, and a timer.

VB.NET:
Public Class Form1
	Dim sw As New Stopwatch
	Dim sec, min, totsec As Integer

	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		sw.Start()
		tmrSeconds.Enabled = True
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		sw.Stop()
		tmrSeconds.Enabled = False
	End Sub

	Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
		sw.Reset()
		tmrSeconds.Enabled = False
		sec = 0
		min = 0
		totsec = 0
		lblSeconds.Text = "0"
	End Sub

	Private Sub tmrSeconds_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSeconds.Tick
		Dim ts As TimeSpan = sw.Elapsed
		sec = Convert.ToInt32(ts.Seconds)
		min = Convert.ToInt32(ts.Minutes)
		totsec = sec + min * 60
		lblSeconds.Text = totsec.ToString
	End Sub

End Class
 
Last edited:
Thanks, John. I missed that one.

Here is the revised Timer code for displaying the seconds as Integer:

VB.NET:
	Private Sub tmrSeconds_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSeconds.Tick
		Dim ts As TimeSpan = sw.Elapsed
		totsec = Convert.ToInt32(ts.TotalSeconds)
		lblSeconds.Text = totsec.ToString
	End Sub

To display as Double, skip the Convert statement and just do this:
lblSeconds.Text = ts.TotalSeconds.ToString
 
Back
Top