Datetime picker date comparision

On the form I have two Datetime picker control
 DTPickerStartTime
DTPickerEndTime

But when user selects the date which is greater than enddate 
error message fires many times


Private Sub DTPickerStartTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPickerStartTime.ValueChanged
        
            If (DTPickerStartTime.Value > DTPickerEndTime.Value) Then
               MsgBox("Start Date should always been less than or equal to end Date")        
                Exit Sub
        
        End If

    End Sub


Answer this question

Datetime picker date comparision

  • shaggydiggs

    Hi, Why don't you try using the validating Event.....

    Does the event valuechanged, fires the error like a loop

  • Zarxrax

    yes. Please execute it and let me know how it works for u
  • Olle Gustafsson

    I assume error mesage is the one you are triggering  Or some oher mesage via the framework

    if you have a problem here then you should do something to end the problem, for example you could set the checkbox to unchecked.

    The DateTimePicker has a property called ShowCheckbox, this will show a checkbox, as soon as you click (select a date) it will auto checkmark the box. So you would change your code like this.

    Private Sub DTPickerStartTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPickerStartTime.ValueChanged 

    If (DTPickerStartTime.Value > DTPickerEndTime.Value) Then 
    if dtpickerstarttime.checked = true then
    MsgBox("Start Date should always been less than or equal to end Date") 
    dtpickerstarttime.checked = false
    Exit Sub 
    end if
    End If 

    Personally I had to make a custom version so that I could handle nulls and set null dates, if the date is nothing (user wants to set date to null) then they would uncheck the checkbox. If they select a date then it auto checks and that gets saved. So this same method could work for your issue too.

  • Datetime picker date comparision