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

Posted on 2011-05-04
Last Modified: 2012-05-11
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.

Question by:bobcann
    LVL 11

    Accepted Solution

    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.
    LVL 11

    Expert Comment

    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.
    LVL 18

    Assisted Solution

    LVL 11

    Expert Comment

    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 :)

    Author Closing Comment

    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!
    LVL 11

    Expert Comment

    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):

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now