Facts:
- I have a TabControl with 2 Tabs, Each tab has 2 DateTimePicker.
- In the Load event, I set the value of all the DTPs.
- All DTPs have ShowCheckBoxes set on true AND Checked set on false.
- When I Execute the program, The DTPs in first tab are OK, but when I check the DTPs on second tab, they show current time, not the time I set on the load event.
Why this happen? How can I avoid it?
I just ran into the same issue using two DateTimePickers. I was able to get both of them to show the correct value by dynamically generating them and adding them to the form.
My ugly workaround to this issue, consists on set active the tab in which the DTP's reside before changing its values, something like this:
It has to do with the Checked property of the datetimepicker. It is usually set to false. At least that was the problem to me.
After setting the datetimepicker.checked to true, it did retrieve the value from settings.
I found out what the problem was here.
The
Value
property only sets a new value if theDateTimePicker
control is visible. Otherwise command is ignored.Test case:
Doesn't work:
Works:
Doesn't appear to have anything to do with programatically adding the picker it seems.
DateTimePicker has some issues with storing and retrieving it's value. I had trouble when binding the Value to a non nullable DateTime - I got NullReferenceExeptions from time to time. No idea why or when. It sometimes just happened and crashed the app.
If you can't get it to work, you can always try using a different picker. The Any+Time(TM) Datepicker/Timepicker AJAX Calendar Widget pulls its value from the associated field, so if you initialize the field with a value, or change a field to have a value (such as in onload), then that's what you'll get when the picker is displayed. And if you have problems with it, just submit a message via the contact page, and it will be addressed ASAP.