C# 手动触发DateTimePicker ComboBox DropDown事件
问题描述:
项目中用到ComboBox.DropDown事件,来打开DateTimePicker?
代码如下:
private void comboBox1_DropDown(object sender, EventArgs e)
{
dateTimePicker1.Focus();
SendKeys.SendWait("%{DOWN}");
comboBox1.Items.Add(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
comboBox1.SelectedIndex=0;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
comboBox1.Items.Add(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
comboBox1.SelectedIndex = 0;
}
{
dateTimePicker1.Focus();
SendKeys.SendWait("%{DOWN}");
comboBox1.Items.Add(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
comboBox1.SelectedIndex=0;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
comboBox1.Items.Add(dateTimePicker1.Value.ToString("dd/MM/yyyy"));
comboBox1.SelectedIndex = 0;
}
或者:
[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;
Int32 x = dateTimePicker1.Width - 10;
Int32 y = dateTimePicker1.Height / 2;
Int32 lParam = x + y * 0x00010000;
PostMessage(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1, lParam);
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;
Int32 x = dateTimePicker1.Width - 10;
Int32 y = dateTimePicker1.Height / 2;
Int32 lParam = x + y * 0x00010000;
PostMessage(dateTimePicker1.Handle, WM_LBUTTONDOWN, 1, lParam);