The Winforms DateTimePicker
has this odd behavior. If you set focus to either the day or the year, then when you tab out of the control and tab back in, focus is not restored to the month as it is by default (even if you reset or change the DateTime
value for the control). There is no elegant way to resolve this as the control does not expose those inner controls in any way. You could send mouse/keyboard clicks to change focus, but it’s way simpler to use this arguably ugly hack. The code below will force the control to recreate itself and thus we reset focus to its default state.
var format = dateTimePicker1.Format; dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.Format = format;
The trick is to force the control to internally reset itself by changing the Format
and then setting it back to what it was. The code above assumes that it is something other than Custom
.
HatsOff man… it really works..thank you.
http://www.kettic.com/winforms_ui/csharp_guide/editor_timepicker.shtml
Where to write this code