以编程方式打开DateTimePicker C#控件

时间:2010-03-11 19:21:19

标签: winforms

如何以编程方式打开DateTimePicker C#控件? 我想通过向控件发送密钥来显示Datetime Picker控件中的Calendar。 我们有办法做到吗?

5 个答案:

答案 0 :(得分:7)

尝试以下

//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);

}

答案 1 :(得分:3)

在我的系统(Windows 7,.NET 35)上,其他解决方案无效。我在MS讨论网站上找到了另一种可行的解决方案。

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);
    }
}

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

用法:

dateTimePicker1.Open();

警告。如果dateTimePicker1DataGridView上的控件(即,如果您尝试在DGV上创建弹出的DatePicker),则无效。如果将控件添加到Form,它确实有效。会发生什么情况是合成的光标“向下”事件将被DGV吞噬,并将当前单元格指针向下移动一个而不是丢弃DTP的日历。

答案 2 :(得分:2)

提供解决方案的信用转到astander,这是一个非常好的扩展:

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);
  }

}

用法:

dateTimePicker1.Open();

这样,您可以随时使用任何DateTimePicker控件以任何形式重复使用您的扩展。

我希望你们中的一些人发现这个简单的技巧很有用。

〜乔

答案 3 :(得分:1)

来源:https://social.msdn.microsoft.com/Forums/windows/en-US/f2f0b213-d57a-46de-b924-e21b7ac0882e/programmatically-open-the-calendar-of-the-datetimepicker-control?forum=winforms 参考 David M Morton https://social.msdn.microsoft.com/profile/david%20m%20morton/?ws=usercard-mini

的回答
//DateTimePicker dtpicker
dtpicker.Select();
SendKeys.Send("%{DOWN}");

"%{DOWN}" 组合键 - Alt 键 (%) +向下箭头 以编程方式触发日期时间选择器的按键按下事件的代码 (特别是在日期时间选择器中点击下拉箭头的事件)

答案 4 :(得分:0)

可接受的答案大部分是正确的,但是您还应该使用:

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

发布WM_LBUTTONDOWN事件之后。

此外,显然WM_LBUTTONUP必须事先定义:

const Int32 WM_LBUTTONUP = 0x0202;

所以我的答案是:

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);
}

这避免了Mark Lakata在Windows 7和/或.NET 3.5中的错误。

原因很简单:原始代码模拟了鼠标按下事件,但没有像单击按钮时那样再次使鼠标按下。

在这方面,您可以自己尝试:如果您按鼠标左键打开DateTimePicker并且不释放按钮,那么您将也无法使用该控件。 / p>

编辑:调整jp2code的答案:

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);
  }

}