For you whom does not like to read, short answer for the problem is: use Value property, instead of Text property.
And now, the story behind this:
I uses DateTimePicker as placeholder for date value in my applications and I uses the Text property because the input and return value is string – no need to do casting – and the text will be formatted accordingly to DateTimePicker.Format property. And if you set the the Text property with the correct format, then the Value property will also updates, very convenient because this way you don’t need to worry about the regional setting on user’s machine.
But it has one behavior that I don’t really like, that is if you set the Text property, it won’t update before the DateTimePicker is drawn. Strangely, and fortunately, the Value property will updated anyway.
See this sample code :
Form FORM = new Form;
DateTimePicker DTP = new DateTimePicker();
DTP.Parent = FORM;
DTP.Format = DateTimePickerFormat.Custom;
DTP.CustomFormat = “dd/MM/yyyy”;
DTP.Text = “06/07/2012”; //set DTP value to July, 6th 2012
MessageBox.Show(DTP.Text); //the text in messagebox is empty string
MessageBox.Show(DTP.Value.ToString(“dd/MM/yyyy”) ; //text in messagebox is 06/07/2012 !!
MessageBox.Show(DTP.Text) ;//the text in messagebox is 06/07/2012
MessageBox.Show(DTP.Value.ToString(“dd/MM/yyyy”) ; //text in messagebox is 06/07/2012
So what’s the deal? Well, sometimes users don’t need to view the form containing the particular DateTimePicker instead they only need to view the date for work efficiency reason. This is a very big deal if your application is being used intensively. So in this kind of scenario, you can use the Value property to get the date, but remember, you need to cast it to string and apply the correct format.
Thanks for reading up to this line, leave comments if you don’t mind.