我有一些DateTimePicker
是一种永不更新的形式。
我试过了Value
and Text
, Invalidate()
进而Update()
并且Refresh()
...
从当前日期开始,他们的价值观似乎没有任何改变!
无论我设置什么,当前日期都是(相对)今天的!
这是 .NET 3.5 的错误还是什么?
(不,我不能在此项目上使用 .NET 4。)
如果您确实想要一些代码,那么这里是:dateTimePicker1.Value = user.BirthDay;
。另外,如果我写MessageBox.Show(user.BirthDay.ToString());
我得到一个漂亮的盒子,告诉用户的生日(我的生日,在我的机器上)。 (所以变量中有一个值...)
我还应该提到它们仅适用于日期而不是时间吗?
好吧,我知道我需要写更多:
首先控件更新的方法订阅了Form.Load
事件。因此,当表单和控件可见且“运行”时,它被调用/触发/调用。
其次,看看这段代码及其结果:
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 ...
控制不好! 1900 不等于 1753!
dateTimePicker1.MaxDate = DateTime.Today;
// In reality, I need it to today's date
MessageBox.Show(dateTimePicker1.MinDate.ToString()); // December 31st, 9998
时间扭曲?奥_奥
不管怎样,整个代码看起来像这样:
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
}
那么,有什么解决办法吗? xC