Thursday, December 22, 2011

How to open DateTime picker C# control programmatically on Button Click

You have to use interop to to send request to windows that show DataTimePicker 
on click of the button


//include namespace section 
using System.Runtime.InteropServices;

//declares
[DllImport("user32.dll")]
private static extern bool PostMessageForCalender(
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 calender dropdown
private void button1_Click(object sender, EventArgs e)
{
Int32 x = dateTimePicker1.Width - 10;
Int32 y = dateTimePicker1.Height / 2;
Int32 lParam = x + y * 0x00010000;

PostMessageForCalender(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1,lParam);

}

No comments :

Post a Comment