Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I have re-written the code now multiple times the issue seems to be with the Focus. All i want to do is for the 2 date time pickers if the user enters date1 greater than date2 then error message and puts the user back on the control and the same for Date2 can be less that date1:

with the code of focus it causes it to execute twice.

Any Ideas?

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 System.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
Posted
Updated 24-Apr-12 10:58am
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-12 14:47pm    
What is supposed to mean "the user in the date time picker"? :-) Could you format the code properly (in "pre" blocks)?
--SA

Wow! Just look at your code: you are comparing Start and Finish and do something, and then you compare same very values again, in a different way. Why if the result of comparison is already known? You also write if result == False Then…. Did you think a bit before doing that? Your result == False is Boolean expression, but result itself is already Boolean.

You are certainly lost in simplest logic. Fix it, before you discuss what executes twice, but indicate what exactly executes twice. Use the debugger before you ask about this problem.

—SA
 
Share this answer
 
1- Function DateCompare trully needs a As Boolean somewhere

2- As SA stated, your logic in DateCompare is over-complicated.

I would use the Validating event of the DateTimePicker :
VB
Private Sub DateTimePicker_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles dtpIn.Validating, dtpOut.Validating
   If (dtpIn.Value > dtpOut.Value)
      ' I would remove the following 5 lines
      If (DirectCast(sender, DateTimePicker).Name = "dtpIn")
         MsgBox("Date In Cannot be after Date Out, Please Amend.")
      Else
         MsgBox("Date Out Cannot be before Date In, Please Amend.")
      End If
      ' End of to be removed
      e.Cancel = True
   End If
End Sub


Setting the Cancel property of the event to true will make that the user won't be able to leave the control without entering a valid date.
 
Share this answer
 
Comments
DaveP35 24-Apr-12 20:45pm    
Cheers Guys,

I have amended the code and the code works as I wanted

The original code was a basically the same but instead of using the validating i had on leave of the event and then focus.
I just couldnt understand how the <field>.Focus called the code to execute twice.

Thanks again for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900