I've run into a very specific bug with the DateTimePicker control in Windows Forms. The control has a custom format (MM-YYYY -> 01/2010) - the month/year of a credit card. Today is the 29th of September. If a user selects the control, and uses the keyboard to set the month to February, the control will throw an ArgumentOutOfRangeException. There is no such date as 29-Feb-2009. This will also happen on days as the 31st, moving to a month with only 30 days.
It'd be easy enough to handle this by setting the day to '01', however a user can click the calendar to manually select the 30th, and then use the keyboard to select February.
How can this exception be caught, when the input is happening on the GUI and not really in code? We've tried to catch it in the TextChanged event, but that is already too late. The exception is already thrown.
Is there any way to handle this case? It would be nice if the control automatically changed the day to the highest value for that month. Or at least if it passed the exception through an event.
(I'm aware that we could trap the KeyPressed event and set the day to 01 each time, but this feels 'hacky').