Chrono problem

Tantie

Member
Joined
Sep 3, 2008
Messages
9
Programming Experience
Beginner
Hello, I'm new on the forum and also in VB.NET.

I'm having a problem with chrono program. I use the system time to start from 00:00:00.000, but when I run the program I've seen that it sometimes start negative?
Can someone help me?

This the code:
VB.NET:
Public Class Form1
    Dim chrono As New DateTime
    Dim effTijd As DateTime

    Private Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click
        effTijd = DateTime.Now
        Timer1.Start()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_honderden.Text = "000"
        txt_seconden.Text = "00"
        txt_minuten.Text = "00"
        txt_uren.Text = "00"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        chrono = DateTime.Now
        txt_honderden.Text = chrono.Millisecond - effTijd.Millisecond
        txt_seconden.Text = chrono.Second - effTijd.Second
        txt_minuten.Text = chrono.Minute - effTijd.Minute
        txt_uren.Text = chrono.Hour - effTijd.Hour
    End Sub

    Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
        Timer1.Stop()
        txt_honderden.Text = "000"
        txt_seconden.Text = "00"
        txt_minuten.Text = "00"
        txt_uren.Text = "00"
    End Sub
End Class
Please feel free to edit the code or give suggestions?
 
Last edited by a moderator:
One of the solutions would be to use the stopwatch object :
VB.NET:
    Dim Chrono As New Stopwatch
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Chrono.Reset()
        Chrono.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Chrono.Stop()
        Console.WriteLine("Ellpased time : {0} seconds", Chrono.Elapsed.Seconds)
    End Sub
 
txt_honderden.Text = chrono.Millisecond - effTijd.Millisecond
txt_seconden.Text = chrono.Second - effTijd.Second
txt_minuten.Text = chrono.Minute - effTijd.Minute
txt_uren.Text = chrono.Hour - effTijd.Hour
Time calculations doesn't work that way. Say if you got 11:59 and then 12:01, your calculatiojn would then give 1 hour and -58 minutes (add them together and result is 2 minutes elapsed). With the .Net Date and Time tools this become real easy:
VB.NET:
Dim ts As TimeSpan = Date.Now - effTijd '(or use Subtract method)
txt_minuten.Text = ts.Minutes
txt_uren.Text = ts.Hours
 
Back
Top