我有一些永远不会更新的DateTimePicker
s。
我已经尝试过Value
和Text
,Invalidate()
然后Update()
以及Refresh()
......
似乎没有什么能改变它们从当前日期开始的价值! 无论我设置什么,当前的日期都是(相对)今天的!
这是.NET 3.5的错误还是什么? (不,我不能在这个项目中使用.NET 4。)
如果你真的想要一些代码,那么它就是:dateTimePicker1.Value = user.BirthDay;
。此外,如果我写MessageBox.Show(user.BirthDay.ToString());
我得到一个很好的盒子告诉用户的生日(我的生日,在我的机器上)。 (所以变量中有一个值...)
我是否还应该提到他们只是为了约会而不是时代?
好的,我知道我需要写更多:
首先,更新控件的方法是订阅Form.Load
事件。因此,当窗体和控件可见并且“正在运行”时,它被调用/ fired / invoked。
其次,看看这段代码及其结果:
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
一个小麻烦有这个麻烦:我的问题是我将DateTimePicker设置为checked = false和(错误地)ShowCheckbox = false;通过这种设置,我可以设置DTPicker我想要的任何值,但它不会使用自己。
对于不更新其可视外观的表单的标准诊断,但是您看到使用调试器更新的属性更新是使用错误的表单实例。像这样例如:
var frm = new Form1(); // Wrong!!
frm.UpdateBirthDay(user.BirthDay);
通过改变你的代码来诊断这个:
dateTimePicker1.Value = user.BirthDay;
this.Show(); // <=== add this
我更新顽固的DateTimePicker以显示其当前值并更新其相关的DataBindings的工作是切换其.Enabled属性。
myDateTimePicker.Enabled = !myDateTimePicker.Enabled;
myDateTimePicker.Enabled = !myDateTimePicker.Enabled;