尝试根据保存在.txt文件中的旧拆分启动秒表

时间:2015-07-30 03:27:15

标签: vb.net

我得到的最接近的是获取.txt文件中的最后一行(保存了一堆拆分),然后将其添加到当前经过的时间,然后在标签框中显示:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Dim lines() As String = IO.File.ReadAllLines(filepath)
    Dim lastline As String

    lastline = lines(lines.Length - 1)

    Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
    Dim pastHours As String
    pastHours = lastline.Substring(0, 2)
    Dim currentHours As Double = elapsed.Hours + pastHours

    Dim pastMinutes As String
    pastMinutes = lastline.Substring(3, 2)
    Dim currentMinutes As Double = elapsed.Minutes + pastMinutes

    Dim pastSeconds As String
    pastSeconds = lastline.Substring(6, 2)
    Dim currentSeconds As Double = elapsed.Seconds + pastSeconds

    Dim pastMilliseconds As String
    pastMilliseconds = lastline.Substring(9, 3)
    Dim currentMilliseconds As Double = elapsed.Milliseconds + pastMilliseconds

    Label1.Text = Format(currentHours, "00") & ":" & Format(currentMinutes, "00") & ":" & Format(currentSeconds, "00") & "." & (currentMilliseconds)
End Sub

唯一的问题是,不是一秒钟内有1000毫秒,一分钟内就是60秒等,它最后会达到60 PLUS。例如,如果保存的最后一次分割是34秒,那么它将持续到显示的94秒之前,它会在它上升一分钟之前,即使这样,它也会在34秒后开始秒数。

1 个答案:

答案 0 :(得分:0)

您可以相互添加TimeSpan个值。在您的情况下,您可以这样做:

Dim pastElapsed As TimeSpan = New TimeSpan(0, pastHours, pastMinutes, pastSeconds, pastMilliseconds)
Dim currentElapsed As TimeSpan = elapsed + pastElapsed
Dim formatted As String = Format(currentElapsed.Hours, "00") & ":" & Format(currentElapsed.Minutes, "00") & ":" & Format(currentElapsed.Seconds, "00") & "." & Format(currentElapsed.Milliseconds, "000")
Label1.Text = formatted