Results 1 to 3 of 3

Thread: VB - Dropdown the calendar of a date time picker control using code

  1. #1
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    VB - Dropdown the calendar of a date time picker control using code

    Using APIs you can dropdown a combo box, but I couldn't find any
    code on showing the calendar on a date time picker. So I wrote
    this small piece of code. Just pass the date time picker you want
    to open. The code can be put in a module, but you need to pass
    the form name also.

    How it works -
    I save the mouse position and move it to the date time picker
    control and position it so it will be ready to click on the dropdown
    arrow button. Then I use the mouse_event API to send the
    mouse click. After that I return the mouse to its original position.
    The mouse coordinates need to be converted to absolute
    coordinates instead of realitive to the form.

    The code to save the mouse position and return it back to
    the original position can be modified to leave the mouse on the
    date time picker control.

    The date time picker control can be added to a project by clicking -
    Projects > Components > and select Microsoft Windows Common
    Controls-2 6.0 (SP4).

    ex. usage.
    All code behind a form - then behind the form.
    Call ClickOnDTP(dtpDueDate)

    All code in a module - then behind the form.
    Call ClickOnDTP(frmTasks.dtpDueDate)

    Let me know what you think.
    Enjoy.

    Code:
    Option Explicit
    '<07/01/2003 - VB/OUTLOOK GURU>
    Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, _
    ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    
    Public Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    
    Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    
    Public Const MOUSEEVENTF_ABSOLUTE = &H8000
    Public Const MOUSEEVENTF_MOVE = &H1
    Public Const MOUSEEVENTF_LEFTDOWN = &H2
    Public Const MOUSEEVENTF_LEFTUP = &H4
    Public Const SM_CXSCREEN = 0 'X Size of screen
    Public Const SM_CYSCREEN = 1 'Y Size of Screen
    
    Public Function ClickOnDTP(ByVal dtpCalendar As DTPicker)
    '<RR 07/01/2003 VB/OUTLOOK GURU>
        Dim rDTP As RECT
        Dim p As POINTAPI
        Dim oldP As POINTAPI
        Dim oldX As Long
        Dim oldY As Long
        
        dtpCalendar.SetFocus
        Call GetWindowRect(dtpCalendar.hwnd, rDTP)
        Call GetCursorPos(oldP)
        oldX = oldP.x
        oldY = oldP.y
        p.x = (rDTP.Right - 5)
        p.y = ((rDTP.Bottom - rDTP.Top) / 2) + rDTP.Top
        Call ScreenToAbsolute(p)
        Call mouse_event(MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE Or MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, p.x, p.y, 0&, 0&)
        Call SetCursorPos(oldX, oldY)
    
    End Function
    
    Public Sub ScreenToAbsolute(lpPoint As POINTAPI)
    '<RR 07/01/2003 - VB/OUTLOOK GURU>
        lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))
        lpPoint.y = lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
    End Sub
    Last edited by RobDog888; Aug 14th, 2004 at 10:34 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

  2. #2
    Member
    Join Date
    Jun 12
    Posts
    36

    Re: VB - Dropdown the calendar of a date time picker control using code

    It can be done easier

    Code:
    Option Explicit
    Private Const VK_DOWN As Long = &H28
    Private Const WM_SYSKEYDOWN As Long = &H104
    
    Private Declare Function SendMessage Lib "user32.dll" _
                             Alias "SendMessageA" ( _
                             ByVal hWnd As Long, _
                             ByVal wMsg As Long, _
                             ByVal wParam As Long, _
                             ByVal lParam As Long) As Long
                             
    Private Sub Command1_Click()
    Call SendMessage(DTPicker1.hWnd, WM_SYSKEYDOWN, VK_DOWN, 0)  
    End Sub
    Last edited by Krool; Jul 30th, 2012 at 02:47 PM.

  3. #3
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 01
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    59,465

    Re: VB - Dropdown the calendar of a date time picker control using code

    Nice! Thanks
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (VBA, VB 6, VB.NET, C#)
    Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista (New)Multiple .NET Framework Versions (New)Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 150 GB HDs RAID 1, 2 GBs DDR2 667 MHz RAM, 3 Viewsonic 17" LCDs, Windows Vista RTM, IE 7, Office 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •