【发布时间】:2019-04-13 05:39:02
【问题描述】:
我有一些DateTimePickers 的表单永远不会更新。
我试过Value 和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
时间扭曲? O_O
反正整个代码是这样的:
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
【问题讨论】:
-
你是第一个报告这个错误的人
-
您是否为日期时间选择器设置了
MinDate?你确定没有人覆盖你的价值吗?因为我认为可以肯定地说框架中没有错误 ;-) -
您是否尝试将值设置为
DateTime.Parse(user.BirthDay.ToString())? -
您会插入您的
BithDay属性代码吗? -
它确实不起作用,否则我不会发布这个问题,对吧?
标签: c# winforms datetimepicker