Open a DateTimePicker C # control programmatically

How to programmatically open a DateTimePicker C # control? I want to show a calendar in a Datetime Picker control by sending keys to the control. Is there a way we can do this?

+7
winforms
source share
4 answers

Try to execute

//part of the usings using System.Runtime.InteropServices; //declares [DllImport("user32.dll")] private static extern bool PostMessage( IntPtr hWnd, // handle to destination window Int32 msg, // message Int32 wParam, // first message parameter Int32 lParam // second message parameter ); const Int32 WM_LBUTTONDOWN = 0x0201; //method to call dropdown private void button1_Click(object sender, EventArgs e) { Int32 x = dateTimePicker1.Width - 10; Int32 y = dateTimePicker1.Height / 2; Int32 lParam = x + y * 0x00010000; PostMessage(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1,lParam); } 
+7
source share

On my system (Windows 7, .NET 35) other solutions did not work. I found another solution on the MS discussion site that really worked.

 using System.Runtime.InteropServices; public static class Extensions { [DllImport("user32.dll", SetLastError = true)] private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); private const uint WM_SYSKEYDOWN = 0x104; public static void Open(this DateTimePicker obj) { SendMessage(obj.Handle, WM_SYSKEYDOWN, (int)Keys.Down, 0); } } 

Source: http://social.msdn.microsoft.com/Forums/windows/en-US/f2f0b213-d57a-46de-b924-e21b7ac0882e/programmatically-open-the-calendar-of-the-datetimepicker-control?forum= winforms

Using:

 dateTimePicker1.Open(); 

Warnings. This will not work if dateTimePicker1 is a control on a DataGridView (i.e. if you try to create a DatePicker popup on DGV). It works if a control is added to Form instead. What will happen is that the event with the synthesized cursor “down” will be swallowed by the DGV and will move the current cell pointer down by one, instead of discarding the DTP calendar.

+3
source share

The loan is sent to astander to provide a solution, which makes a very nice extension:

 using System.Linq; using System.Runtime.InteropServices; public static class Extensions { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int PostMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, Int32 lParam); public static void Open(this DateTimePicker obj) { const int WM_LBUTTONDOWN = 0x0201; int width = obj.Width - 10; int height = obj.Height / 2; int lParam = width + height * 0x00010000; // VooDoo to shift height PostMessage(obj.Handle, WM_LBUTTONDOWN, 1, lParam); } } 

Using:

 dateTimePicker1.Open(); 

That way, you can reuse the extension anytime, anytime, in any form using any DateTimePicker control.

I hope some of you find this simple trick useful.

~ Joe

+2
source share

The accepted answer is mostly correct, however you should also use:

PostMessage(dateTimePicker1.Handle, WM_LBUTTONUP, 1,lParam);

After the publication of the WM_LBUTTONDOWN event.

Also, obviously, WM_LBUTTONUP must be predefined:

const Int32 WM_LBUTTONUP = 0x0202;

So my answer is:

 using System.Runtime.InteropServices; //declares [DllImport("user32.dll")] private static extern bool PostMessage( IntPtr hWnd, // handle to destination window Int32 msg, // message Int32 wParam, // first message parameter Int32 lParam // second message parameter ); const Int32 WM_LBUTTONDOWN = 0x0201; const Int32 WM_LBUTTONUP = 0x0202; //method to call dropdown private void button1_Click(object sender, EventArgs e) { Int32 x = dateTimePicker1.Width - 10; Int32 y = dateTimePicker1.Height / 2; Int32 lParam = x + y * 0x00010000; PostMessage(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1,lParam); PostMessage(dateTimePicker1.Handle, WM_LBUTTONUP, 1,lParam); } 

This avoids the Mark Lakata error in Windows 7 and / or .NET 3.5.

The reason is simple: the original code simulates a mouse click event, but does not raise the mouse button again, as when a button was clicked.

In this regard, you can try it yourself: if you press the left mouse button to open the DateTimePicker and do not release the button, you will also not be able to use the control.

Edit: Adapting jp2code response:

 using System.Runtime.InteropServices; public static class Extensions { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int PostMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, Int32 lParam); public static void Open(this DateTimePicker obj) { const int WM_LBUTTONDOWN = 0x0201; const int WM_LBUTTONUP = 0x0202; int width = obj.Width - 10; int height = obj.Height / 2; int lParam = width + height * 0x00010000; // VooDoo to shift height PostMessage(obj.Handle, WM_LBUTTONDOWN, 1, lParam); PostMessage(obj.Handle, WM_LBUTTONUP, 1, lParam); } } 
0
source share

All Articles