I have a dateTimePicker. In order to write this DateTimePicker content to my DataBase, i get the value with :
myDateTimePicker.selectedDate.value.
It's ok when the user selected manually the date.
But...when the user type the date directly in the DateTimePicker, the SelectedDate.value
give me the older value. The good value, is in the .Text
property.
Is there a way to synchronize the .Text
with the .SelectedValue.value
?
Maybe I need to read an other property ?
The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format.
This happens because validation is only performed when a control loses focus. Normally controls like this are on dialog boxes (i.e. ShowDialog), and the dialog box is completed by activating an OK button (directly or indirectly through the AcceptButton property). This causes the control to lose focus and validate itself.
If you're using the DateTimePicker on a modeless window, you'll want to remove focus from it before using its selected value so that it performs validation.
One can use these conversions in the code for runtime processing. "on change" methods can be used too but this is the quickest way. They can even be used in those methods too.
date to text conversion
setdate.Text = setdate.SelectedDate.ToString();
text to date conversion (if the format is acceptable)
setdate.SelectedDate = DateTime.Parse(setdate.Text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With