Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimePicker.selectedDate different than DateTimePicker.Text

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 ?

like image 951
Walter Fabio Simoni Avatar asked Mar 15 '13 14:03

Walter Fabio Simoni


People also ask

What is DateTimePicker type?

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.


2 Answers

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.

like image 140
Tergiver Avatar answered Oct 22 '22 06:10

Tergiver


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);
like image 44
Yılmaz Durmaz Avatar answered Oct 22 '22 04:10

Yılmaz Durmaz