2015-11-14 107 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