DateTimePicker ValueChanged 事件在没有更改时第一次触发

发布于 2024-08-07 04:56:22 字数 267 浏览 4 评论 0原文

Winforms 2.0。将 DateTimePicker 粘贴到表单上。默认为今天。单击下拉箭头以显示日历,然后单击“今天”。 ValueChanged 事件确实会触发,即使它已经设置为今天。

我想做的是复制相同的功能 - 我想将其(在代码中)重置为今天,下次我通过日历单击“今天”时,我希望触发 ValueChanged 事件(就像第一次一样) )。

如果上述不可能,我希望每当我在日历上选择日期时总会触发一些事件(无论是否有更改)。

听起来很简单,肯定有人有答案吗?

Winforms 2.0. Stick a DateTimePicker on a form. It defaults to TODAY. Click on the dropdown arrow to show the Calendar, and click on TODAY. The ValueChanged event DOES FIRE, even though it is already set to today.

What i would like to do is replicate this same functionality - I would like to reset it (in code) to today, and next time i click on today via the Calendar i want that ValueChanged event to fire (just like it did the first time).

If the above is not possible, i want some event that always fires whenever i pick a date on the Calendar (regardless if there is a change or no change).

Sounds really simple, surely someone has the answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-08-14 04:56:22

您所看到的事实是 DateTimePicker 存储其值的时间部分以及日期这一事实的副作用。

因此,当您的表单首次初始化时,DateTimePicker 将使用 DateTime.Now 进行初始化。当您从下拉日历中选择“今天”时,您将值更改为 DateTime.Today。

如果您不关心时间组件和/或始终将值读取为 dateTimePicker1.Value.Date,那么您可以使用 DateTime.Now 在代码中初始化控件,并且当用户选择 Today 时将触发 ValueChanged 事件从日历上。否则,您可能会考虑捕获 CloseUp 事件,每当日历控件关闭时都会触发该事件(无论值是否更改)。

What you're seeing there is a side-effect of the fact that DateTimePicker stores the time component of its value as well as the date.

So when your form first initializes, the DateTimePicker is being initialized with DateTime.Now. When you then select "Today" from its drop-down calendar, you're changing the value to DateTime.Today.

If you don't care about the time component and/or you're always reading the value as dateTimePicker1.Value.Date, then you could initialize the control in code with DateTime.Now and the ValueChanged event will fire when the user selects Today from the calendar. Otherwise you might consider catching the CloseUp event, which is fired whenever the calendar control is closed (regardless of whether the value changed).

黎夕旧梦 2024-08-14 04:56:22

我不是一个win形式的程序员,但是看看是否有Day_Render事件。
这应该每天都会发生……至少有一个用于基于网络的开发,但我认为它是相同的。因此寻找 Render() 事件。

Im not a win form programmer, but see if there is a Day_Render event.
That should fire for each day...at least there is one for web based development, but I would think it is the same. So look for Render() event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文