C# 日期时间选择器 ->使用 updown &挑选
我有日期时间选择器,我想使用向上和箭头来选择日期(下拉日历),或者如果我右键单击日期时间选择器,则会显示日期选择器(下拉日历)。 我怎样才能做到这一点?
I have datetime picker and I want use updown and the arrow for pick the date (dropdown calendar) or if I right click on the datetime picker the date picker (dropdown calendar) will show.
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实现 DateTimePicker 的本机 Windows 控件几乎没有什么花哨的功能。您可以发送一条消息来强制关闭下拉日历,但没有一条消息可以强制将其打开。这需要变得粗略,可以通过按键 Alt+Down 打开下拉菜单。这使得 MouseUp 事件的事件处理程序能够正常工作:
它很可靠,在这种情况下焦点总是很好。
The native Windows control that implements DateTimePicker has very few bells and whistles. There's a message you can send to force the drop-down calendar to be closed but there isn't one to force it to be opened. This calls for getting crude, the drop-down can be opened with a keystroke, Alt+Down. Which makes this event handler for the MouseUp event work:
It's reliable, focus is always good in this case.
您必须向按钮或控件发送一条 Windows 消息,该消息不会由控件公开。
本文有模拟按下按钮的一个很好的例子。
You have to send a windows message to the button or the control, it is not exposed by the control.
This article has a good example of simulating a press on the button.
从一般意义上讲,您正在谈论绑定到事件。在这种情况下,您有兴趣绑定到键盘和鼠标事件。
因此请查找有关按键、按键事件和单击事件的参考资料。
在按键事件中,您可以过滤您感兴趣的按键或按键组合的类型(向上/向下键)。
在单击事件处理程序中,您将检查按下了哪个鼠标按钮(左键和右键等)。
一旦捕获并过滤了感兴趣的事件,您就可以触发日期时间选择器上的下拉操作。要显示日历,您可以在日期时间选择器上调用 InvokeOnClick。
编辑:我假设您正在对控件进行子类化。
In a very general sense you are talking about binding to events. In this case you are interested in binding to both keyboard and mouse events.
So look up the reference material on keypress, keydown events and click events.
Within keypress events you can filter for the type of key or key combination that interests you in this the up / down keys.
Within the click event handler you would check to see which mouse button was held down ( left and right and so forth).
Once you have captured and filtered the event of interest, you can just trigger the drop down action on the date time picker. To display the calendar you can call InvokeOnClick on the datetimepicker.
EDIT: I'm assuming you're subclassing the control.
有一种方法可以做到这一点,但它有点像黑客(我不太喜欢它)。正如已经指出的,您可以使用
SendMessage
弹出日历,但是当ShowUpDown
为 true 时(我理解这就是您所拥有的),这不起作用。因此,右键单击时,您必须先将 ShowUpDown 设置为 false,然后再显示日历。要再次显示上下,请在日期时间选择器的 ValueChanged 和 Leave 事件中将
ShowUpDown
设置为 true。There is a way to do this, but it's a bit of a hack (I wouldn't like it too much). As pointed out already, you can pop up the calendar by using
SendMessage
, but that does not work whenShowUpDown
is true (which I understand is what you have).So, on right-click, you'll have to set ShowUpDown to false first and then show the calendar. To show the up-down again, set
ShowUpDown
to true in the ValueChanged and the Leave event of the datetime picker.