Stopwatch .NET (problema increment)


(Alex Mieila) #1

Salutare, revin si eu cu o problema in vb.net.
Vreau sa fac un stopwatch care sa inregistreze orele, minutele si secundele. Problema este ca in momentul in care se fac 00:00:59 ar trebui sa arate dupa o secunda 00:01:00, dar la mine continua la infinit secundele si nu se resteaza.
devofrum_proof

Code:

Public Class Form1
    Private stopwatch_donone As New Diagnostics.Stopwatch
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
    Private Sub don1_timer_Tick(sender As Object, e As EventArgs) Handles don1_timer.Tick
        Dim donone_elapsed As TimeSpan = stopwatch_donone.Elapsed
        donone_timerwatch.Text = String.Format("{0:00}:{1:00}:{2:00}", Math.Floor(donone_elapsed.TotalHours), Math.Floor(donone_elapsed.TotalMinutes), Math.Floor(donone_elapsed.TotalSeconds))
    End Sub
    Private Sub donone_startbutton_Click(sender As Object, e As EventArgs) Handles donone_startbutton.Click
        If donone_startbutton.Text = "Start" Then
            don1_timer.Start()
            stopwatch_donone.Start()
            donone_startbutton.Text = "Stop"
        ElseIf donone_startbutton.Text = "Stop" Then
            don1_timer.Stop()
            stopwatch_donone.Stop()
            donone_startbutton.Text = "Start"
        End If
    End Sub
    Private Sub donone_resetbutton_Click(sender As Object, e As EventArgs) Handles donone_resetbutton.Click
        stopwatch_donone.Reset()
        donone_timerwatch.Text = "00:00:00"
    End Sub

(Adrian) #2

Probabil ar fi cazul sa folosesti alta formatare, si nu Total. https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.7.2

In alta ordine de idei, VB? The horror! Macar jaful de C# daca ar fi…


(Alex Mieila) #3

Asta mi-a fost la indeamana :slight_smile: Chiar nu stiu de ce am folosit asta.


(cosmos) #4

Trebuie sa incrementezi minutele cand ajungi la a 60 a secunda. Acelasi lucru si pt ora cand ajungi la minutul 60

https://www.dreamincode.net/forums/topic/95412-create-a-simple-stopwatch/


(Adrian) #5

Nu trebuie, TimeSpan are grija de toate, doar ca a folosit gresit TotalSeconds in loc de Seconds, etc.


(cosmos) #6

Da
Acum am vazut


(Alex Mieila) #7

Am rezolvat multumesc frumos :slight_smile: Trebuia folosit released.Hours, seconds etc instead of Total :slight_smile:

Multumesc!