我尝试了许多我现在都记不住的条件。 结果是:
0:60:01
Private Sub stopwatch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopwatch.Tick
lock()
ts.Text = ts.Text + 1
If ts.Text < 9 Then
ts.Text = "0" & ts.Text
ElseIf ts.Text = 60 Then
If tm.Text < 9 Then
ts.Text = "00"
tm.Text = "0" & tm.Text + 1
Else
ts.Text = "00"
tm.Text = tm.Text + 1
End If
ElseIf tm.Text = 59 And ts.Text = 59 Then ' this is where I am having trouble with
If th.Text < 9 Then
ts.Text = "00"
tm.Text = "00"
th.Text = "0" & th.Text + 1
Else
th.Text = th.Text + 1
End If
End If
End Sub
如何计算到59秒?因为在58之后,它会重置。
答案 0 :(得分:0)
更改此行:
ElseIf tm.Text = 59 And ts.Text = 59 Then
到
ElseIf tm.Text = 60 And ts.Text = 60 Then
这可能看似违反直觉,但是当你在循环之前执行数学运算时,你没有达到59,但只有58。
您可以通过将代码更改为以下内容来显着简化代码:
' Get the elapsed time as a TimeSpan value.
Dim ts As TimeSpan = stopwatch.Elapsed
th.Text = String.Format("{0:00}", ts.Hours)
tm.Text = String.Format("{0:00}", ts.Minutes)
ts.Text = String.Format("{0:00}", ts.Seconds)
答案 1 :(得分:0)
以下是我的工作,感谢您的见解。
If ts.Text < 10 Then
ts.Text = "0" & ts.Text
End If
If ts.Text = 60 Then
If tm.Text < 9 Then
ts.Text = "00"
tm.Text = "0" & tm.Text + 1
Else
ts.Text = "00"
tm.Text = tm.Text + 1
End If
End If
If tm.Text = 60 And ts.Text = "00" Then
If th.Text < 10 Then
ts.Text = "00"
tm.Text = "00"
th.Text = "0" & th.Text + 1
Else
ts.Text = "00"
tm.Text = "00"
th.Text = th.Text + 1
End If
End If