2015-11-14 105 views
-1

我正在为我的同事和我工作一个简单的项目在工作(物流公司)使用。ArgumentOutOFRangeException当比较时间

让我稍微解释一下,让我的问题更容易一点。

每条路线代表一个国家有截止日期。在本例中,我使用Route 114. Route 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 
+0

对于发生什么行的想法会有所帮助。定时器不够精确,它们在设置时会熄灭。有时它们可​​能会在某个关键点(截止日期* .7 ???)之后打勾,导致负值(即时间已过)。你也可以使用1个计时器@ 850毫秒来简化计算,并且每次评估一批货物以确定状态 – Plutonix

+0

@Plutonix它通常发生在这些行上:pb113.Value = value113。取决于我在DateTimePickers中插入的时间。 –

+0

...我们是否假设'pb113'是一个'DateTimePicker'?这可能是我解释的 - 定时器迟了一点。由于计时器代码是相同的,因此1应该足够了(可能会刷新以查看编辑的第一条评论) – Plutonix

回答

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