Vb.net 比较时间时ArgumentOutOfRange异常

Vb.net 比较时间时ArgumentOutOfRange异常,vb.net,Vb.net,我正在做一个简单的项目,供我和我的同事在工作(物流公司)中使用 让我解释一下,让我的问题简单一点 每条路线代表一个有最后期限的国家。在本例中,我使用路线114。114号航线代表荷兰,订单应在当地时间xx:xx:xx完成。 我正在使用DateTimePicker,这样用户可以选择截止日期,并在ProgressBar达到70%时收到警告(在本例中,标签变为红色) 到目前为止,我的代码仍然有效,但有时会抛出一个错误,即: “-4758”的值对“最大值”无效“最大值”必须更大 大于或等于0。参数名称:最

我正在做一个简单的项目,供我和我的同事在工作(物流公司)中使用

让我解释一下,让我的问题简单一点

每条路线代表一个有最后期限的国家。在本例中,我使用路线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
公开课截止日期
专用路由114截止日期为布尔值=False
公共函数GetTimeDifference(ByVal EndTime作为DateTime,ByVal StartTime作为DateTime)作为整数
Dim span As TimeSpan=EndTime.TimeOfDay-StartTime.TimeOfDay
Dim结果为整数=CInt(跨度总计秒)
返回结果
端函数
私有子tm114_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理tm114.Tick
‘114号干线’
Dim value114为Integer=pb114.Maximum-GetTimeDifference(DateTimePicker1.Value,DateTime.Now)
如果值114>pb114。最大值,则
tm114.Stop()
如果结束
如果值114小于pb114,则为最小值
tm114.Stop()
出口接头
如果结束
pb114.值=值114
如果没有路由114截止日期,也没有路由pb114.值>=pb114.最大值*0.7,则
Route114Deadline=True
lb114.ForeColor=颜色。红色
如果结束
端接头
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
pb114.最小值=0
pb114.max=GetTimeDifference(DateTimePicker1.Value,DateTime.Now)
tm114.Start()
端接头
末级

通过一些帮助找到了解决方案

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
公共类表单1
专用路由114截止日期为布尔值=False
公共函数GetTimeDifference(ByVal EndTime作为DateTime,ByVal StartTime作为DateTime)作为整数
Dim span As TimeSpan=结束时间-开始时间
Dim结果为整数=CInt(跨度总计秒)
返回结果
端函数
私有子tm114_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理tm114.Tick
‘114号干线’
'从现在开始到结束的秒数
Dim value114为整数=GetTimeDifference(DateTimePicker1.Value,DateTime.Now)
'检查秒数是否小于0
如果值114小于0,则
tm114.Stop()'停止计时器
pb114.Value=0'将progressbar设置为0,以防最后一个值是1秒,现在小于1
MessageBox.Show(“时间(114)到了!!!”)显示消息或将标签文本更改为allert表示时间到了
出口接头
如果结束
pb114.Value=value114'将进度条设置为新的秒数
如果没有路由114截止日期和pb114。值为0,则
pb114.最大值=秒
tm114.间隔=500
tm114.Start()
其他的
MessageBox.Show(“选择的截止时间已过”)
如果结束
端接头
末级

了解它发生在哪一行会有所帮助。计时器不太精确,设置时会准确地响。有时他们可能会在某个关键点(截止日期*.7°?)后勾选,导致负值(即时间已过)。您还可以在850毫秒时使用1个计时器来简化此过程,并每次评估装运集合以确定status@Plutonix它通常发生在以下行:pb113.Value=value113。根据我在日期时间选择器中插入的时间……我们是否假设
pb113
是一个
DateTimePicker
?这可能就是我所解释的——计时器走得有点晚。由于计时器代码相同,1应该足够(可能刷新以查看对第一条注释的编辑)
pb113
是我正在使用的
ProgressBar
的名称,在本例中是Route113的ProgressBar。错误发生在这些行上。我更新了我的问题。这个问题与DateTimePicker(标题)无关。您的代码没有检查值是否小于pb最小值;您需要检查是否超过关闭计时器的最大值