2

我正在启动小时、分钟和秒

然后将其转换为总毫秒数。

然后我正在做的是从 Elapsed MilliSeconds 中减去 Total MilliSeconds

IEelms = cms - e.Milliseconds

(评估的毫秒 = 计算的毫秒。- Stopwatch.elapsedMilliseconds)

然后将评估的毫秒转换回 HH:MM:SS:MS 格式。

但由于一些逻辑错误,它无法正常工作,这就是为什么我需要一些帮助。请帮帮我这是我的代码:

        Dim dd As Integer = 0
        Dim mm As Integer = 0
        Dim hh As Integer = 0
        Dim ss As Integer = 0
        Dim ms As Integer = 0
        Dim cms As Long = 0
        Dim elms As Long = 0

    Dim stopwatch As System.Diagnostics.Stopwatch = New System.Diagnostics.Stopwatch
    Dim dt As DispatcherTimer = New DispatcherTimer

    Private Sub StartButton_Click(sender As Object, e As RoutedEventArgs) Handles StartButton.Click

                hh = Hours.Text * 60 * 60 * 1000
                mm = Minutes.Text * 60 * 1000
                ss = Seconds.Text * 1000
                cms = hh + mm + ss
                hh = mm = ss = 0
                Start()
    End Sub

    Private Sub Start()

                dt.Interval = New TimeSpan(0, 0, 0, 0, 10)
                AddHandler dt.Tick, AddressOf ontimertick
                stopwatch.Start()
                dt.Start()

            End If
    End Sub

    Private Sub ontimertick()


            Dim e As New TimeSpan
            e = stopwatch.Elapsed

                elms = cms - e.Milliseconds
                ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
                mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
                hh = elms \ (1000 * 60 * 60)
                elms = elms Mod 1000

                MicroSeconds.Text = elms.ToString("00")
                Seconds.Text = ss.ToString("00")
                Minutes.Text = mm.ToString("00")
                Hours.Text = hh.ToString("00")
    End Sub
4

2 回答 2

0

错误在这里:

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
hh = elms \ (1000 * 60 * 60)
elms = elms Mod 1000

肯定是:

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
 elms=elms-(ss*1000)
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
 elms=elms-(mm*1000)
hh = elms \ (1000 * 60 * 60)
 elms=elms-(hh*1000)
elms = elms Mod 1000

好的?

于 2012-09-20T17:42:49.380 回答
0

你把事情复杂化了。您需要做的就是将总时间存储为一个TimeSpan对象。然后,您可以从中减去StopWatch.Elapsed时间,因为它也是一个TimeSpan. 然后,TimeSpan计算结果的对象将包含剩余时间。例如:

' Get the total desired time for count down
Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))

' Later, get the time left and display on screen
Dim timeLeft As TimeSpan = total - stopwatch.Elapsed
Hours.Text = timeLeft.TotalHours
Minutes.Text = timeLeft.TotalMinutes
Seconds.Text = timeLeft.TotalSeconds

显然,只是这样调用Integer.Parse是不安全的,因为文本框可能包含非数字值。您将需要添加代码来处理它。例如,如果您只想在输入无效时默认为零,您可以执行以下操作:

Dim totalHours As Integer
Dim totalMinutes As Integer
Dim TotalSeconds As Integer
Integer.TryParse(Hours.Text, totalHours)
Integer.TryParse(Minutes.Text, totalMinutes)
Integer.TryParse(Seconds.Text, totalSeconds)
Dim total As New TimeSpan(totalHours, totalMinutes, totalSeconds)

或者,如果您想显示验证错误消息,您可以执行以下操作:

Dim total As TimeSpan
Try
    total = New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))
Catch ex As FormatException
    MessageBox.Show("Entries must be numeric.")
End Try
于 2012-09-20T17:47:22.267 回答