如何禁用 DateTimePicker 控件上的某些日期?
如何禁用 DateTimePicker
中选定的日期,以便用户无法选择它们?我知道这在 Web 窗体中是可能的,但在 Windows 窗体中我无法执行此操作。我怎样才能实现这个目标?
How can I disable selected dates in a DateTimePicker
so that a user cannot select them? I know it's possible in Web Forms but in Windows Forms I'm unable to do this. How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您执行此操作的难易程度取决于您想要限制的日期。例如,如果您只想指定要选择的有效日期范围,则可以使用 MinDate 和 MaxDate 属性来设置范围。但是,如果您想在一定范围内挑选某些日子(例如,没有周末),则没有内置方法可以执行此操作。
您可以找到具有此功能的第三方控件,或者您可以尝试通过向 ValueChanged 事件,如果用户选择了某个值,则强制当前日期时间为最后一个值(您必须缓存该值)根据您的业务逻辑,这是非法的……但这是一种不太理想的方法。
The ease with which you can do this will depend on the dates you want to restrict. For instance, if you all you want to do is specify a range of valid dates to pick, then you can use the MinDate and MaxDate properties to set the bounds. If however, you want to cherry pick certain days within a range (for instance, no weekends), there is no built in method for doing this.
You could either find a third party control with this functionality, or you could try to hack it a bit by adding an event handler to the ValueChanged event, and forcing the current date time to the last value (which you'd have to cache) if they user picked something that was illegal according to your business logic... but this is a less than ideal way to do it.