Tags

, , , , , , , ,


csharp

Okay, this is a strange behavior of DateTimePicker.
I have this scenario here:
– Normal windows Form is created
– DateTimePicker added to that form
– Form displayed with ShowDialog
– DateTimePicker will automatically display today’s date by default.
– Form is closed
– Set value of DateTimePicker to other date programmatically
– Form displayed again with ShowDialog
– DateTimePicker still showing today’s date

This is quite frustrating behavior. Luckily the solution is simple (but took me some researches).

To fix this issue, the DateTimePicker.Checked properties need to be set to True before changing the date. In my case, setting it right after creating the DateTimePicker object did not fix the issue.

So the fixed workflow should be like this:
– Normal windows Form is created
– DateTimePicker added to that form
– Form displayed with ShowDialog
– DateTimePicker will automatically display today’s date by default.
– Form is closed
-Set DateTimePicker.Checked = True
– Set value of DateTimePicker to other date programmatically
– Form displayed again with ShowDialog
– Viola, DateTimePicker now showing the correct date.

Advertisements