How can I handle windows forms datetimepicker ArgumentOutOfRangeException?

I am having a specific error with the DateTimePicker control in Windows Forms. The control has its own format (MM-YYYY → 01/2010) - month / year of a credit card. Today is September 29th. If the user selects a control and uses the keyboard to set the month to February, the control will throw an ArgumentOutOfRangeException. There is no such date as February 29, 2009. This will also happen on the 31st day, moving to a month with 30 days.

It would be easy to handle this by setting the day to “01,” however, the 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 input occurs in the graphical interface, and not in the code? We tried to catch it in the TextChanged event, but it is already too late. An exception has already been selected.

Is there a way to handle this case? It would be nice if the control automatically changed the day to the maximum value for this month. Or at least if it passed an exception through an event.

(I know that we could capture the KeyPressed event and set the day to 01 each time, but that seems to be “hacked”).

+5
source share
3 answers

, , DatePicker ... , , " t ?

, , . - .

EDIT: , , Application.ThreadException, , .

+5

DateTimePicker ShowUpDown true, , ?

+1

, / . , . DateTimePicker - , .

DateTimePicker , .NET-, , DateTimePicker - . , , ...

If I can: I suggest MaskedTextBoxwith a mask set to '00 / 0000 '(credit card format) and then with confirmation of entry.

0
source

All Articles