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;