2010-03-11 36 views

Trả lời

7

Hãy thử như sau

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

} 
+0

Điều này dường như bị hỏng trên Windows 7 hoặc .NET35. Lịch bật lên, nhưng nó không đáp ứng. Di chuột qua điều khiển không làm bất cứ điều gì và khi bạn nhấp vào một ngày, lịch sẽ biến mất nhưng không cập nhật .Value của điều khiển DateTimePicker. –

+0

Kinda tối ma thuật! Works.Thanks! – fnc12

2

tín dụng đi vào astander cho việc cung cấp các giải pháp, mà làm cho một phần mở rộng rất đẹp:

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

} 

Cách sử dụng:

dateTimePicker1.Open(); 

Bằng cách này, bạn có thể sử dụng lại Tiện ích mở rộng của mình bất kỳ lúc nào bạn muốn, hết lần này đến bất kỳ hình thức nào bằng cách sử dụng bất kỳ điều khiển DateTimePicker nào.

Tôi hy vọng một số bạn thấy rằng mẹo đơn giản hữu ích.

~ Joe

+1

hahaha, VooDoo thật buồn cười – Henrique

3

Trên hệ thống của tôi (Windows 7, .NET 35) các giải pháp khác không hoạt động. Tôi tìm thấy một giải pháp khác trên trang thảo luận MS đã hoạt động.

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

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

Cách sử dụng:

dateTimePicker1.Open(); 

cảnh báo. Điều này sẽ không hoạt động nếu dateTimePicker1 là một Điều khiển trên DataGridView (nghĩa là nếu bạn cố gắng tạo một DatePicker bật lên trên DGV). Nó hoạt động nếu Control được thêm vào Form thay thế. Điều gì sẽ xảy ra là con trỏ tổng hợp "xuống" sự kiện sẽ được nuốt bởi DGV, và sẽ di chuyển con trỏ tế bào hiện tại xuống một thay vì thả thả Lịch của DTP.

Các vấn đề liên quan