2012-04-24 126 views
0

我已經得到了開始日期來驗證結束日期和顯示消息後,但是當我把下面的代碼.focus它似乎運行代碼兩次 我已經嘗試了一切 句柄是假 嘗試,它沒有工作 我也把它變成一個新的線程,沒有工作 併成一個函數,沒有工作 IM在我結束智慧在這一個代碼運行兩次

這裏是代碼

Function DateCompare(ByVal Start As Date, ByVal Finish As Date, ByVal WhichDate As String) 
    If WhichDate = "DateIn" Then 
    If DateTime.Compare(Start, Finish) > 0 Then 
     MsgBox("Dates Are Invalid : Date In Cannot be after Date Out, Please Amend", vbExclamation, "Dates Are Incorrect") 
     Return False 
     Exit Function 
    Else 
     Return True 
    End If 
    Else 
    If DateTime.Compare(Finish, Start) < 0 Then 
     MsgBox("Dates Are Invalid : Date Out Cannot be before Date In, Please Amend", vbExclamation, "Dates Are Incorrect") 
     Return False 
     Exit Function 
    Else 
     Return True 
    End If 
    End If 
End Function 

Private Sub dtpDateTimeIn_leave(ByVal sender As Object, ByVal e As EventArgs) Handles dtpDateTimeIn.Leave 
    Dim result As Boolean 
    result = DateCompare(dtpDateTimeIn.Value, dtpDateTimeOut.Value, "DateIn") 
    If result = False Then 
    dtpDateTimeIn.Focus() 
    End If 
End Sub 

Private Sub dtpDateTimeOut_leave(ByVal sender As Object, ByVal e As EventArgs) Handles dtpDateTimeOut.Leave 
    Dim result As Boolean 
    result = DateCompare(dtpDateTimeIn.Value, dtpDateTimeOut.Value, "DateOut") 
    If result = False Then 
    dtpDateTimeOut.Focus() 
    End If 
End Sub 

回答

0

嘗試使用代替Validating事件。他們是這樣做的:

Private Sub dtpDateTimeIn_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles dtpDateTimeIn.Validating 
    e.Cancel = Not DateCompare(dtpDateTimeIn.Value, dtpDateTimeOut.Value, "DateIne") 
End Sub 

Private Sub dtpDateTimeOut_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles dtpDateTimeOut.Validating 
    e.Cancel = Not DateCompare(dtpDateTimeIn.Value, dtpDateTimeOut.Value, "DateOut") 
End Sub