Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimePicker control adding functionality upon focus

I'm just looking to see if the functionality I would like to add to the DateTimePicker control is actually possible.

What I would like to do is upon the user tabbing to the DateTimePicker having the date picker drop down appear so they can start entering the date rather than having to click the arrow to make the date picker appear.

I haven't seen anything on this from MSDN but I may have missed it, is this possible?

like image 663
Criel Avatar asked Apr 30 '26 22:04

Criel


2 Answers

From Programmatically open the calendar of the DateTimePicker control, you can try this:

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

To tab through the drop down, try something like this:

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
like image 74
LarsTech Avatar answered May 03 '26 11:05

LarsTech


in C# you can create a extension to DropDown the DateTimePicker, i apologize who created this because i totally forgot :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);
        }

The usage:

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

here's the VB .NET, first you need to create a Module and paste this code

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

and the usage is:

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

and that`s it :)

like image 25
spajce Avatar answered May 03 '26 09:05

spajce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!