我的代碼如下:東西是我的時間跨度碼不對勁
Private Sub tbRcvrDepartTime_textchanged(sender As Object, e As EventArgs) Handles tbRcvrDepartTime.TextChanged
'Converts the 90 Receiver Arrival & Departures Date & Times to a string for comparison
Dim raTime As String = tbRcvrArriveTime.Text 'Takes the Time only String and converts to string
Dim raDate As String = dpRcvrArriveDate.Text 'Takes the DateTimePicker and converts date to string
Dim raDateString = String.Concat(raDate, " ", raTime) 'Puts together the Date & Time into one continuous string
'Dim raDateFormat As String = "MM-dd-yyyy HH:mm" 'Sets the String to Date style Format
Dim raResultDate As Date = CDate(raDateString) 'Finalizes the String for use in below comparison
Dim rdTime As String = tbRcvrDepartTime.Text 'Takes the Time only String and converts to string
Dim rdDate As String = dpRcvrDepartDate.Text 'Takes the DateTimePicker and converts date to string
Dim rdDateString = String.Concat(rdDate, " ", rdTime) 'Puts together the Date & Time into one continuous string
'Dim rdDateFormat As String = "MM-dd-yyyy HH:mm" 'Sets the String to Date Format
Dim rdResultDate As Date = CDate(rdDateString) 'Finalizes the String for use in below comparison
'Checks to see if 2 or more hours have elapsed since Receiver Arrival/Departure Date & Time
Dim elapsedR As TimeSpan = rdResultDate.Subtract(raResultDate)
tbRcvrDepartTime.BackColor = If(elapsedR.TotalMinutes > 120, Color.LightPink, Color.White)
End Sub
兩個raTime & rdTime是獨立文本框。 兩個raDate & rdDate是datetimepickers。
當我最初運行代碼「live」時,我看到的第一條記錄顯示正確。一旦我移動到另一個記錄,這將退出窗口......我得到隨機結果,如果> 120分鐘過後,它不會將背景顏色更改爲正確的顏色。其他時候,當經過120分鐘後,它會改變背景顏色。有時在背景顏色上沒有變化,或者當它不應該變色時。我試圖最初使用TotalHours來完成此操作,但遇到了相同的結果。它是隨機的,並不一致。我已經爲此工作了2天,結果沒有任何差異。我的想法是需要有一種方法來刷新每個新記錄加載時的rdResultDate & raResultDate信息,但我無法通過我的代碼知識來做到這一點。
代碼必須能夠考慮到新的日期是否存在 - 即raDate:11/01/2016和raTime:23:46和 rdDate:11/02/2016和rdTime:03:00 - 這將超過2小時(或120分鐘),並且應該讀作「真」,並在超過2小時(或120分鐘)時改變背景顏色。
然而,如果以下是真的: raDate:2016年11月1日和raTime:23點46和
rdDate:2016年11月2日和rdTime:01:00,這將不超過2小時(或120分鐘),並應讀取「假」,並不會改變背景顏色。
試過了ParseExact,它絕不會喜歡那樣的。我禁用rdDateFormat和raDateFormat變量希望這可以解決,但沒有運氣。我已更新原始消息中的代碼以反映建議的更改。我仍然想出背景的隨機着色,所以看起來elapsedR變量是不正確的 - 即使我在調試中運行它,格式顯示正確。 –