here is code for 2 different ways of doing the same.
of course timespan is a lot shorter method!
you need to dim StartTime And StopTime AS DateTime Rather than TimeSpan Though
Code:
Dim LBL As New Label
Dim Btn As New Button
Dim startTime As DateTime
Dim stopTime As DateTime
Dim duration As TimeSpan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Btn = New Button
Btn.Text = "Start Time"
Btn.Name = "BtnStart"
Btn.Location = New Point(10, 10)
Btn.Size = New Size(40, 40)
Me.Controls.Add(Btn)
AddHandler Btn.Click, New System.EventHandler(AddressOf Btn_Click)
Btn = New Button
Btn.Text = "End Time"
Btn.Name = "BtnEnd"
Btn.Location = New Point(120, 10)
Btn.Size = New Size(40, 40)
Me.Controls.Add(Btn)
AddHandler Btn.Click, New System.EventHandler(AddressOf Btn_Click)
LBL = New Label
LBL.Name = "lblStart"
LBL.Location = New Point(10, 60)
LBL.Text = Now
Me.Controls.Add(LBL)
LBL = New Label
LBL.Name = "lblEnd"
LBL.Location = New Point(120, 60)
Me.Controls.Add(LBL)
LBL = New Label
LBL.Name = "lblEnd"
LBL.Location = New Point(110, 100)
Me.Controls.Add(LBL)
LBL = New Label
LBL.Name = "lblEnd"
LBL.Location = New Point(110, 140)
Me.Controls.Add(LBL)
LBL = New Label
LBL.Name = "lblEnd"
LBL.Location = New Point(110, 180)
Me.Controls.Add(LBL)
LBL = New Label
LBL.Name = "lblEnd"
LBL.Location = New Point(110, 220)
Me.Controls.Add(LBL)
End Sub
Private Sub Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If (CType(sender, Button).Name()) = "BtnStart" Then
Me.Controls(2).Text = Now
Else
Me.Controls(3).Text = Now
Me.Controls(4).Text = DateDiff(DateInterval.Hour, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
Me.Controls(5).Text = " Hours:" & DateDiff(DateInterval.Hour, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
Me.Controls(6).Text = " Minutes:" & DateDiff(DateInterval.Minute, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
Me.Controls(7).Text = " Seconds:" & DateDiff(DateInterval.Second, CDate(Me.Controls(2).Text), CDate(Me.Controls(3).Text))
startTime = Me.Controls(2).Text
stopTime = Me.Controls(3).Text
duration = startTime.Subtract(stopTime)
MsgBox(duration.ToString)
End If
End Sub
Just copy And Pase Between The Class Declaration And THe End Class