[英]How not to show calendar in the DateTimePicker when user clicks DropDown button
In the DateTimePicker
when user clicks DropDown
button, a calendar gets displayed. 在
DateTimePicker
当用户单击DropDown
按钮时,将显示日历。 I don't want to show calendar on the DropDown event. 我不想在DropDown事件上显示日历。
Is there some way to overwrite this event and to show custom form when user clicks DropDown button?? 是否有某种方法可以覆盖此事件并在用户单击DropDown按钮时显示自定义表单?
I don't want to show calendar on the DropDown event.
我不想在DropDown事件上显示日历。
You can prevent the calendar from being displayed by setting the control's ShowUpDown
property to true
. 您可以通过将控件的
ShowUpDown
属性设置为true
来阻止显示日历。 Instead of the drop-down calendar, it will instead show a spin button to the side of the DateTimePIcker control that allows the user to select the desired date/time. 它将代替下拉日历,而是在DateTimePIcker控件的一侧显示一个旋转按钮,允许用户选择所需的日期/时间。
Is there some way to overwrite this event and to show custom form when user clicks DropDown button??
是否有某种方法可以覆盖此事件并在用户单击DropDown按钮时显示自定义表单?
Well, yeah, I guess so. 嗯,是的,我想是的。 You can handle the
DropDown
event , which gets raised when the control shows the drop-down calendar. 您可以处理
DropDown
事件 ,当控件显示下拉日历时会引发该事件 。 In this event handler, you could display whatever form you wanted. 在此事件处理程序中,您可以显示所需的任何表单。 The only problem is that the drop-down calendar is still going to be shown as normal.
唯一的问题是下拉日历仍然会正常显示。 I don't know how you prevent that from happening.
我不知道你是如何防止这种情况发生的。
You can, however, hack around it by forcing the drop-down calendar closed again. 但是,您可以通过强制再次关闭下拉日历来破解它。 To do that, P/Invoke the
SendMessage
function and send the control the DTM_CLOSEMONTHCAL
message . 为此,P / Invoke
SendMessage
函数并向控件发送DTM_CLOSEMONTHCAL
消息 。
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private const uint DTM_FIRST = 0x1000;
private const uint DTM_CLOSEMONTHCAL = DTM_FIRST + 13;
public static void CloseCalendar(this DateTimePicker dtp)
{
SendMessage(dtp.Handle, DTM_CLOSEMONTHCAL, IntPtr.Zero, IntPtr.Zero);
}
But I can't imagine the reason you would want to do this. 但我无法想象你想要这样做的原因。 If you don't want a calendar or a spin-button control, then you don't want a DateTimePicker.
如果您不想要日历或旋转按钮控件,那么您不需要DateTimePicker。 Just use a regular combo box or a drop-down button, then you can display whatever form you want when it is clicked.
只需使用常规组合框或下拉按钮,即可在单击时显示所需的任何表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.