[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-04
6
Medium Priority
?
384 Views
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.

-Bob
0
Comment
Question by:bobcann
  • 4
6 Comments
 
LVL 11

Accepted Solution

by:
brutaldev earned 1000 total points
ID: 35694123
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.
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35694217
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.
0
 
LVL 18

Assisted Solution

by:lludden
lludden earned 1000 total points
ID: 35694260
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:brutaldev
ID: 35694491
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 :)
0
 

Author Closing Comment

by:bobcann
ID: 35698981
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!
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35700826
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

873 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