比较时间时ArgumentOutOFRangeException

时间:2015-11-14 14:05:12

标签: vb.net

我正在为同事和我工作一个简单的项目(工作公司)。

让我解释一下,让我的问题更容易一些。

每条路线代表一个有截止日期的国家/地区。在此示例中,我使用Route 114.路线114表示荷兰,订单应在xx:xx:xx当地时间结束。 我使用DateTimePicker,因此用户可以选择截止日期,并在ProgressBar达到70%时收到警告(在这种情况下标签变为红色)。

到目前为止,我的代码已经有效,但有时会抛出错误说:

' -4758'对于'最高'无效。 '最大'必须更大 大于或等于0.参数名称:最大值

我是业余爱好者,但在某些情况下看起来时间倒计时,从而产生负值。

    Public Class Deadlines

    Private Route114Deadline As Boolean = False

    Public Function GetTimeDifference(ByVal EndTime As DateTime, ByVal StartTime As DateTime) As Integer
        Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay
        Dim result As Integer = CInt(span.TotalSeconds)
        Return result
    End Function

    Private Sub tm114_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm114.Tick
        ' ROUTE 114 '
        Dim value114 As Integer = pb114.Maximum - GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
        If value114 > pb114.Maximum Then
            tm114.Stop()
        End If
        If value114 < pb114.Minimum Then
            tm114.Stop()
            Exit Sub
        End If
        pb114.Value = value114
        If Not Route114Deadline AndAlso pb114.Value >= pb114.Maximum * 0.7 Then
            Route114Deadline = True
            lb114.ForeColor = Color.Red
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        pb114.Minimum = 0
        pb114.Maximum = GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
        tm114.Start()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在一些帮助下找到解决方案!

Public Class Form1
        Private Route114Deadline As Boolean = False

        Public Function GetTimeDifference(ByVal EndTime As DateTime, ByVal StartTime As DateTime) As Integer
            Dim span As TimeSpan = EndTime - StartTime
            Dim result As Integer = CInt(span.TotalSeconds)
            Return result
        End Function

        Private Sub tm114_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm114.Tick
            ' ROUTE 114 '

            'get the seconds from now to end
            Dim value114 As Integer = GetTimeDifference(DateTimePicker1.Value, DateTime.Now)

            'check if the seconds are less than 0
            If value114 < 0 Then
                tm114.Stop() 'stop the timer
                pb114.Value = 0 'set the progressbar to 0 just in case the last value was 1 second and is now less than 1
                MessageBox.Show("Time for (114) is up!!!") 'show a message or change the text of a label to allert that time is up
                Exit Sub
            End If

            pb114.Value = value114 'set the progressbar to new amount of seconds

            If Not Route114Deadline AndAlso pb114.Value <= pb114.Maximum * 0.7 Then
                Route114Deadline = True
                lb114.ForeColor = Color.Red
            End If
        End Sub

        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            'get the number of seconds from now to end
            Dim secs As Integer = GetTimeDifference(DateTimePicker1.Value, DateTime.Now)

            'make sure there is at least 1 second before setting the progressbar maximum and starting the timer
            If secs > 0 Then
                pb114.Maximum = secs
                tm114.Interval = 500
                tm114.Start()
            Else
                MessageBox.Show("The chosen deadline time has already passed")
            End If
        End Sub
    End Class