Re: displaying elapsed time?
You should set a start datetime, the use a timer to fire every 100ms (or so). In the tick-event you substract the startdate time from the current date time and you update the label
Re: displaying elapsed time?
I was thinking of that approach already, I just wanted to see if there was a better way of going about it first.
Re: displaying elapsed time?
The way I would do it is use 5 labels, 00:00:00, the first 00 would be named Hours, the second 00 would be named Minutes, and the third 00 would be named Seconds. On the form loadup make the timer start, this timer should have an interval of 1 second or in other words just make it 1,000. Then in the timer's tick sub you would do something along the lines of this;
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Seconds.Text <= 9 Then
Seconds.Text = 0 & Seconds.Text + 1
ElseIf Seconds.Text >= 10 Then
Seconds.Text = Seconds.Text + 1
End If
If Seconds.Text = 60 Then
If Minutes.Text <= 9 Then
Minutes.Text = 0 & Minutes.Text + 1
ElseIf Minutes.Text >= 10 Then
Minutes.Text = Minutes.Text + 1
End If
Seconds.Text = 0
ElseIf Minutes.Text = 60 Then
If Hours.Text <= 9 Then
Hours.Text = 0 & Hours.Text + 1
ElseIf Hours.Text >= 10 Then
Hours.Text = Hours.Text + 1
End If
Minutes.Text = 0
End If
End Sub
Re: displaying elapsed time?
Ok maybe I will try that.
Re: displaying elapsed time?
i'd use a stopwatch:
vb.net Code:
Public Class Form1
Dim sw As New Stopwatch
Private WithEvents tmr As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sw.Start()
tmr.Interval = 1000 '1 second
tmr.Start()
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds)
End Sub
End Class
Re: displaying elapsed time?
Thanks paul. I tried both, the stopwatch approach is a little easier on performance, which is needed in this application Thank you both.