Link to home
Start Free TrialLog in
Avatar of bobcann
bobcannFlag for United States of America

asked on

How to Display the Calendar When Clicking the "Text" Area of the DateTimePicker Control

In a Vb.Net Visual Studio 2008 application the calendar must be displayed when the "text" portion of the DateTimePicker control is clicked?

Thank you for any help.

-Bob
ASKER CERTIFIED SOLUTION
Avatar of brutaldev
brutaldev
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm definitely tired cause I totally misread the title. I'll write you a code sample, it won't be easy but it's totally possible using the MouseDown event and sending a click to the button that makes the calendar pop-up.
SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Had the solution, just couldn't get it posted so here goes anyway.

1. Create a class to host you extension method that will perform a click on the control to pop-up the calendar:
 
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace MyExtensions
  Public NotInheritable Class DateTimePickerExtensions
    Private Sub New()
    End Sub
    <DllImport("user32")> _
    Private Shared Function SendMessage(hWnd As IntPtr, uMsg As UInteger, wParam As Integer, lParam As Integer) As Integer
    End Function

    Const WM_LBUTTONDOWN As Integer = &H201

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Sub ShowCalendar(picker As DateTimePicker, currentEvent As MouseEventHandler)
      If picker IsNot Nothing Then
        ' Remove any existing event to prevent an infinite loop.
        If currentEvent IsNot Nothing Then
          RemoveHandler picker.MouseDown, currentEvent
        End If

        Dim x As Integer = picker.Width - 10
        Dim y As Integer = picker.Height \ 2
        Dim lParam As Integer = x + y * &H10000

        SendMessage(picker.Handle, WM_LBUTTONDOWN, 1, lParam)
        If currentEvent IsNot Nothing Then
          AddHandler picker.MouseDown, currentEvent
        End If
      End If
    End Sub
  End Class
End Namespace

Open in new window

2. Attach the MouseDown event onto your control (double-click from Properties in the designer) and simply call ShowCalendar() from you control instance passing in the MouseDown method you are in:
Private Sub dateTimePicker1_MouseDown(sender As Object, e As MouseEventArgs)
  dateTimePicker1.ShowCalendar(AddressOf dateTimePicker1_MouseDown)
End Sub

Open in new window

3. Remember to import the namespace so that the extension method is available:
 
Imports MyExtensions

Open in new window



The way this works is by clicking the button that would normally open the calendar. You need to detach the event because otherwise it will fire for the automatic click that is happening in the extension and infinite loop.

Hope this makes up for the previous blunder :)
Avatar of bobcann

ASKER

lludden, you posted a link to the correct answer. It was use immediately by a coworker. Thank you.

But, brutaldev, you did so much work! It's not used, but we are assuming it works. Thank you, too.

I leaned quite a bit in this thread. Thanks again!
No problem. I actually wanted a more complete solution that will work on devices as well and fixed the bugs of other people's examples so I ended up writing an article about it (for the C# guys): http://www.brutaldev.com/post/2011/05/05/Show-calendar-on-click-DateTimePicker-control.aspx