C# 日期时间选择器 ->使用 updown &挑选

发布于 2024-12-17 23:28:40 字数 83 浏览 1 评论 0原文

我有日期时间选择器,我想使用向上和箭头来选择日期(下拉日历),或者如果我右键单击日期时间选择器,则会显示日期选择器(下拉日历)。 我怎样才能做到这一点?

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 技术交流群。

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

发布评论

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

评论(4

叶落知秋 2024-12-24 23:28:40

实现 DateTimePicker 的本机 Windows 控件几乎没有什么花哨的功能。您可以发送一条消息来强制关闭下拉日历,但没有一条消息可以强制将其打开。这需要变得粗略,可以通过按键 Alt+Down 打开下拉菜单。这使得 MouseUp 事件的事件处理程序能够正常工作:

    private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) SendKeys.Send("%{down}");
    }

它很可靠,在这种情况下焦点总是很好。

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:

    private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) SendKeys.Send("%{down}");
    }

It's reliable, focus is always good in this case.

白云不回头 2024-12-24 23:28:40

您必须向按钮或控件发送一条 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.

浴红衣 2024-12-24 23:28:40

从一般意义上讲,您正在谈论绑定到事件。在这种情况下,您有兴趣绑定到键盘和鼠标事件。

因此请查找有关按键、按键事件和单击事件的参考资料。

在按键事件中,您可以过滤您感兴趣的按键或按键组合的类型(向上/向下键)。

在单击事件处理程序中,您将检查按下了哪个鼠标按钮(左键和右键等)。

一旦捕获并过滤了感兴趣的事件,您就可以触发日期时间选择器上的下拉操作。要显示日历,您可以在日期时间选择器上调用 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.

作死小能手 2024-12-24 23:28:40

有一种方法可以做到这一点,但它有点像黑客(我不太喜欢它)。正如已经指出的,您可以使用 SendMessage 弹出日历,但是当 ShowUpDown 为 true 时(我理解这就是您所拥有的),这不起作用。

因此,右键单击时,您必须先将 ShowUpDown 设置为 false,然后再显示日历。要再次显示上下,请在日期时间选择器的 ValueChanged 和 Leave 事件中将 ShowUpDown 设置为 true。

[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const uint WM_SYSKEYDOWN = 0x104;

void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        this.dateTimePicker1.ShowUpDown = false;
        SendMessage(dateTimePicker1.Handle, WM_SYSKEYDOWN, (int)Keys.Down, 0);
    }
    else
    {
        this.dateTimePicker1.ShowUpDown = true;
    }
}

// Connect this to ValueChanged and Leave events.
private void resetDateTimePickerShowUpDown(object sender, EventArgs e)
{
    this.dateTimePicker1.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 when ShowUpDown 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.

[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const uint WM_SYSKEYDOWN = 0x104;

void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        this.dateTimePicker1.ShowUpDown = false;
        SendMessage(dateTimePicker1.Handle, WM_SYSKEYDOWN, (int)Keys.Down, 0);
    }
    else
    {
        this.dateTimePicker1.ShowUpDown = true;
    }
}

// Connect this to ValueChanged and Leave events.
private void resetDateTimePickerShowUpDown(object sender, EventArgs e)
{
    this.dateTimePicker1.ShowUpDown = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文