Friday, May 1, 2009

sample vb .net timer

so I needed a timer/stopwatch, googled around, found all sorts of over complicated timer examples doing /60/60 to get the hour etc...
this code requires a form (form1) 3 buttons and a label. thats it,
1 start button 1 pause button 1 reset button and 1 label to display the time

//begin vb code //

Public Class Form1
Public agentLoggedTime As Integer = 0
Private Sub btnTimerStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimerStart.Click
tmrTimer.Enabled = True
End Sub

Private Sub btnTimerPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimerPause.Click
tmrTimer.Enabled = False
End Sub

Private Sub btnTimerReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimerReset.Click

Dim resetanswer As MessageBoxButtons = MessageBoxButtons.YesNo
resetanswer = MessageBox.Show("Reset timer to 0?", "Confirm Timer Reset", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If resetanswer = System.Windows.Forms.DialogResult.Yes Then
tmrTimer.Enabled = False
agentLoggedTime = 0
Else

End If
tmrTimerDisplay.Text = TimeSpan.FromSeconds(agentLoggedTime).ToString

End Sub


Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
agentLoggedTime += 1
tmrTimerDisplay.Text = TimeSpan.FromSeconds(agentLoggedTime).ToString
End Sub
End Class

No comments:

Post a Comment