stopwatch - sample program

Try Running It


VB.NET

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

' global variables

Dim starttime, stoptime As Date

Dim elapsedtime As TimeSpan

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

starttime = Now()

txtStart.Text = Format(starttime, "H:mm:ss") '24-hour time

txtStop.Text = ""

txtElapsed.Text = ""

btnStop.Visible = True

btnStart.Visible = False

End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click

stoptime = Now()

txtStop.Text = Format(stoptime, "H:mm:ss")

elapsedtime = stoptime.Subtract(starttime)

' convert elapsedtime/TimeSpan to String, then to Date w/CDate, then format it

txtElapsed.Text = Format(CDate(elapsedtime.ToString), "H:mm:ss")

btnStop.Visible = False

btnStart.Visible = True

End Sub

End Class

 


VB 6.0

Dim StartTime As Variant
Dim EndTime As Variant
Dim ElapsedTime As Variant

Private Sub btnStart_Click()
StartTime = Now
txtStart.Text = Format(StartTime, "hh:mm:ss")
txtStop.Text = ""
txtElapsed.Text = ""
btnStop.Enabled = True
btnStart.Enabled = False
End Sub

Private Sub btnStop_Click()
EndTime = Now
ElapsedTime = EndTime - StartTime
txtStop.Text = Format(EndTime, "hh:mm:ss")
txtElapsed.Text = Format(ElapsedTime, "hh:mm:ss")
btnStart.Enabled = True
btnStop.Enabled = False
End Sub

Private Sub Form_Load()

End Sub