0

終了日と表示メッセージの後かどうかを確認するための開始日を既に取得していますが、次のコードを配置すると.focusコードが2回実行されるようです。新しいスレッドに、機能せず、関数にも機能しませんでした

ここにコードがあります

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
4

1 に答える 1

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
于 2012-04-24T18:13:24.623 に答える