|
-
Aug 26th, 2012, 01:57 PM
#1
Thread Starter
Addicted Member
displaying elapsed time?
I am wondering if there is a simple way to displaying time elapsed as is it is happening. Instead of getting the date time at start and subtracting from datetime at end. I am looking for a 00:00:00 format as in hrs:min:sec. I thought about maybe adding a timer and somehow converting the milliseconds into the others. Or same with using the stopwatch class.
My main issue is displaying it in the 00:00:00 format as it happens.
-
Aug 26th, 2012, 02:11 PM
#2
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
-
Aug 26th, 2012, 02:41 PM
#3
Thread Starter
Addicted Member
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.
-
Aug 26th, 2012, 04:00 PM
#4
Junior Member
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
Last edited by Providence; Aug 26th, 2012 at 04:24 PM.
Reason: Didn't read question completely
-
Aug 26th, 2012, 05:24 PM
#5
Thread Starter
Addicted Member
Re: displaying elapsed time?
Ok maybe I will try that.
-
Aug 27th, 2012, 06:05 AM
#6
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
Last edited by .paul.; Aug 27th, 2012 at 06:20 AM.
Reason: error
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 27th, 2012, 11:05 AM
#7
Thread Starter
Addicted Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|