DateTimePicker 更改值不会触发值更改事件 - Winforms
我面临着 DateTimePicker 控件的奇怪情况。我有一个具有短日期格式的 DateTimePicker 控件。
我按以下方式在文本框中设置此控件的值:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
textBox1.Text = dateTimePicker1.Value.ToShortDateString();
}
现在,当我尝试更改月份(控件中的第一部分)时,如果我输入两位数字,它将触发 ValueChanged 事件,否则不会。
比如说,我尝试输入第 6 个月,并在键盘上按 6,这次 ValueChanged 事件不会触发。但是当我按 0 和 6 时,它就会触发该事件。如果我输入 11 或 12,它将触发 ValueChanged 事件。这同样适用于日期部分。
简而言之,当我在日期或月份部分输入单个数字时,它不会触发 ValueChanged 事件,但当我输入两位数字时,它会触发。谁能告诉我为什么它会这样?这是 DTP 的预期行为吗?
即使我输入个位数,如何让它触发该事件?
I'm facing weird situation with DateTimePicker control. I've a DateTimePicker control with short date format.
I'm setting the value of this control in the textbox in the following way:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
textBox1.Text = dateTimePicker1.Value.ToShortDateString();
}
Now, when I try to change the month, which is first section in the control, if I enter two digits it fires ValueChanged event otherwise not.
Say, for example I'm trying to enter 6th month and I press 6 on keyboard, this time ValueChanged event doesn't fire. But when I press 0 and 6 then it fires that event. If I enter 11 or 12 it fires ValueChanged event. Same applies to date section as well.
In short it doesn't fire ValueChanged event when I enter single digit in Date or Month section but it fires when I enter two digits. Can anybody tell me why it is behaving this way? Is it expected behaviour of DTP?
How do I make it fire that event even when I enter single digit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了保持一致性和简单性,该字段采用 2 位数字,因此应等待输入这两个数字来触发事件。如果您输入 1 代表一月,即使它不知道您可能输入十月 (0)、十一月 (1) 或十二月 (2) 的另一个数字,它也会触发,
我希望如果您输入 6 并按 Tab 键离开,事件将会触发。
尽管您可能可以在
TextChanged
事件上执行某些操作,但如果您键入一个数字,该事件就会被触发,尽管我不确定在使用鼠标选择日期时它是否会起作用(最好检查一下我记得不久前我遇到了它不发射的问题)For consistency and simplicity the field takes 2 digits and so should wait for both to be entered to fire the event. Should it fire when you enter 1 to represent January even though it does not know you might enter another digit for Oct (0), Nov (1) or Dec (2)
I would expect if you entered 6 and tab away the event would fire.
Although you might be able to do something on the
TextChanged
event which should get fired if you type a single digit although I am not sure it will work when using the mouse to select the date (best to check that as I recall a while back I had issues with it not firing)