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.

Who is Participating?
brutaldevConnect With a Mentor Commented:
Firstly have you considered using System.Forms.DateTimePicker control instead?

If this doesn't quite do what you want then you can do what this guy did but hook up your popup to the Click event of the text box rather than using a separate button.
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.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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 :)
bobcannAuthor Commented:
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):
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.