DateTimePicker控件在焦点上添加功能

时间:2012-12-21 16:09:23

标签: vb.net winforms datetimepicker

我只是想看看我想要添加到DateTimePicker控件的功能是否真的可行。

我想要做的是在用户标签显示DateTimePicker时出现日期选择器下拉列表,以便他们可以开始输入日期,而不必单击箭头以显示日期选择器。< / p>

我从MSDN上没有看到任何内容,但我可能错过了,这可能吗?

2 个答案:

答案 0 :(得分:2)

Programmatically open the calendar of the DateTimePicker control开始,您可以尝试:

Private Sub DateTimePicker1_Enter(ByVal sender As Object, ByVal e As EventArgs) _
                                  Handles DateTimePicker1.Enter
  SendKeys.Send("%{DOWN}")
End Sub

要通过下拉列表,请尝试以下操作:

Private Sub DateTimePicker1_KeyDown(ByVal sender As Object, _
                                    ByVal e As KeyEventArgs) _
                                    Handles DateTimePicker1.KeyDown
  If e.KeyCode = Keys.Tab Then
    SendKeys.Send("%{F4}")
    Me.SelectNextControl(DateTimePicker1, True, True, True, True)        
  End If
End Sub

答案 1 :(得分:1)

在C#中你可以为DropDown创建DateTimePicker的扩展,我道歉是谁创建了这个,因为我完全忘了:D

public static void OpenDateTime(this DateTimePicker obj)
        {
            const int WM_LBUTTONDOWN = 0x0201;
            int width = obj.Width - 10;
            int height = obj.Height / 2;
            int lParam = width + height * 0x00010000; // VooDoo to shift height
            PostMessage(obj.Handle, WM_LBUTTONDOWN, 1, lParam);
        }

用法:

private void dateTimePicker1_Enter(object sender, EventArgs e)
{
    dateTimePicker1.OpenDateTime();
}

这是VB .NET,首先你需要创建一个模块并粘贴这段代码

Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices

Module Module1
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Function PostMessage(hwnd As IntPtr, wMsg As Int32, wParam As Int32, lParam As Int32) As Integer
    End Function
    <Extension()>
    Public Sub OpenDateTime(obj As DateTimePicker)
        Const WM_LBUTTONDOWN As Integer = &H201
        Dim width As Integer = obj.Width - 10
        Dim height As Integer = obj.Height / 2
        Dim lParam As Integer = width + height * &H10000
        PostMessage(obj.Handle, WM_LBUTTONDOWN, 1, lParam)
    End Sub
End Module

,用法是:

Private Sub DateTimePicker1_Enter(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.Enter
    DateTimePicker1.OpenDateTime()
End Sub

就是这样:))