DateTimePicker never updates!

Vercas picture Vercas · Nov 13, 2010 · Viewed 13.1k times · Source

I have some DateTimePickers in a form that never update.
I've tried Value and Text, Invalidate() and then Update() and also Refresh()...

Nothing seems to change their values from the current date!
No matter what I set, the current dates are (relatively)today's!

Is this a .NET 3.5 bug or what?
(No, I cannot use .NET 4 on this project.)


If you really want some code, then here it is: dateTimePicker1.Value = user.BirthDay;. Also, if I write MessageBox.Show(user.BirthDay.ToString()); I get a nice box telling the user's birthday (my birthday, on my machine). (So there is a value in the variable...)


Should I also mention that they are only for dates and not times?


Ok, I see I need to write more:

First of all, the method in which the control is updated is subscribed to the Form.Load event. Consequently, it is called/fired/invoked when the form and the controls are visible and "running".

Secondly, look at this pieces of code and their result:

MessageBox.Show(user.BirthDay.ToString()); // Shows 12.12.1995 (in my regional format)
dateTimePicker1.Value = user.BirthDay; // assigned to 12.12.1995
MessageBox.Show(dateTimePicker1.Value.ToString()); // Shows today's date!

That's not nice... The output is today's date. (By today I mean the day in which the code was ran.)

dateTimePicker1.MinDate = new DateTime(1900,1,1); // January 1st, 1900
MessageBox.Show(dateTimePicker1.MinDate.ToString()); // January 1st, 1753 ...

Bad control! 1900 doesn't equal to 1753!

dateTimePicker1.MaxDate = DateTime.Today;
// In reality, I need it to today's date
MessageBox.Show(dateTimePicker1.MinDate.ToString()); // December 31st, 9998

Time warp? O_O

Anyway, the whole code looks like this:

public void Form_Load(object sender, EventArgs e)
{
    this.user = User.Load(path);
    // this.user is a field.
    // path is a static field which holds the absolute path of the file in which is serialized that data of the user.

    MessageBox.Show(user.BirthDay.ToString()); // Shows 12.12.1995 (in my regional format)
    dateTimePicker1.Value = user.BirthDay; // assigned to 12.12.1995
    MessageBox.Show(dateTimePicker1.Value.ToString()); // Shows today's date!

    dateTimePicker1.MinDate = new DateTime(1900,1,1); // January 1st, 1900
    MessageBox.Show(dateTimePicker1.MinDate.ToString()); // January 1st, 1753 ...

    dateTimePicker1.MaxDate = DateTime.Today;
    MessageBox.Show(dateTimePicker1.MinDate.ToString()); // December 31st, 9998
}

So, any solution? xC

Answer

jelinek.01 picture jelinek.01 · Jan 28, 2011

One small hint with this trouble: my problem was that I had the DateTimePicker set to checked=false and (by mistake) ShowCheckbox=false; With this setup I could set to DTPicker whatever value I wanted, but it won't udate itself.