0

OK, so I am trying to add some functionality to the DateTimePicker control in my application. The affect that I want is to set the max date without it affecting what the user is typing in.

For example, if the value of the DateTimePicker is set to the current day, and the max date is set to the current day also, if the user edits the day and it is a greater number than the current day, it will reset that value. This would be annoying as the normal way to write a date is either day or month first (dependant on your country).

So, my idea was to set the max date on the drop down of the calender to today's date and set it to the maximum when the calendar closes again. This with extra validation to submit the datetime within the parameters should make sense. Here is the code:

Private Sub dtpDateTime_KeyUp(sender As Object, e As KeyEventArgs) Handles dtpDateTime.KeyUp
    If e.KeyCode = Keys.Enter Then TriggerUpdate()
End Sub

Private Sub dtpDateTime_DropDown(sender As Object, e As EventArgs) Handles dtpDateTime.DropDown
    dtpDateTime.MaxDate = Now.Date
End Sub

Private Sub dtpDateTime_CloseUp(sender As Object, e As EventArgs) Handles dtpDateTime.CloseUp
    dtpDateTime.MaxDate = #12/31/9998#
End Sub

This does not seem to work as I think the calendar opens before this event is run.

Is there another way I can do this as I am running out of ideas?

Weebie
  • 230
  • 1
  • 14
  • try `dtpDateTime.MaxDate = DateTime.Today` ` – letsjak Sep 02 '14 at 15:07
  • The calendar works just fine. What of course cannot work is editing the date in the DTP. Especially the KeyUp event is unwise, you are guaranteed to see invalid dates. You cannot avoid the need to implement the Validating event to catch bad entries. – Hans Passant Sep 02 '14 at 15:17

0 Answers0