我试图使用Visual Basic创建一个秒表,但我必须在不使用计时器功能的情况下进行,而是使用循环来代替它。当我点击停止按钮时,秒表标签上的时间应该转移到列表框中作为圈(我还没到那么远)。但我想让秒表实际上开始滴答我应该使用4个嵌套循环一毫秒,秒,分钟,一小时,但我不知道该怎么做。任何帮助都会很棒。下面是我到目前为止,但我没有使用4个嵌套循环,因为我不知道如何把它放进去。
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
System.Threading.Thread.Sleep(1)
Application.DoEvents()
Dim Milliseconds, Seconds, Minutes, Hours As New Integer
For Miliseconds = 1 To 1000
If Miliseconds = 1000 Then
Seconds += 1
End If
Next
For Seconds = 1 To 59
If Seconds = 60 Then
Minutes += 1
End If
Next
For Minutes = 1 To 59
If Minutes = 60 Then
Hours += 1
End If
Next
For Hours = 1 To 23
lblRacingTimer.Text = Hours & "" & ":" & "" & Minutes & "" & ":" & "" & Seconds & "" & ":" & "" & Milliseconds & "" & ":" & "" & ""
Next
答案 0 :(得分:0)
这是您的首发实施,您需要添加取消和其他功能。您可以使用timeSpan获取经过的小时/分钟/秒/毫秒并正确格式化:
Private Sub Stopwatch(Seconds As Integer)
Dim startTime As DateTime
Dim endTime As DateTime
Dim currentTime As DateTime
Dim timeSpan As TimeSpan
startTime = DateTime.Now
endTime = startTime.AddSeconds(Seconds)
currentTime = startTime
Do While currentTime < endTime
currentTime = DateTime.Now
timeSpan = currentTime - startTime
Loop
End Sub
这将使秒表运行12小时:
Stopwatch(43200)