sczdavos
sczdavos

Reputation: 2063

C# datetime picker -> use updown & pick

I have datetime picker and I want use updown and the arrow for pick the date (dropdown calendar) or if I right click on the datetime picker the date picker (dropdown calendar) will show. How can I do that?

Upvotes: 2

Views: 4800

Answers (4)

Hans Passant
Hans Passant

Reputation: 941605

The native Windows control that implements DateTimePicker has very few bells and whistles. There's a message you can send to force the drop-down calendar to be closed but there isn't one to force it to be opened. This calls for getting crude, the drop-down can be opened with a keystroke, Alt+Down. Which makes this event handler for the MouseUp event work:

    private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Right) SendKeys.Send("%{down}");
    }

It's reliable, focus is always good in this case.

Upvotes: 3

Gert Arnold
Gert Arnold

Reputation: 109118

There is a way to do this, but it's a bit of a hack (I wouldn't like it too much). As pointed out already, you can pop up the calendar by using SendMessage, but that does not work when ShowUpDown is true (which I understand is what you have).

So, on right-click, you'll have to set ShowUpDown to false first and then show the calendar. To show the up-down again, set ShowUpDown to true in the ValueChanged and the Leave event of the datetime picker.

[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const uint WM_SYSKEYDOWN = 0x104;

void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        this.dateTimePicker1.ShowUpDown = false;
        SendMessage(dateTimePicker1.Handle, WM_SYSKEYDOWN, (int)Keys.Down, 0);
    }
    else
    {
        this.dateTimePicker1.ShowUpDown = true;
    }
}

// Connect this to ValueChanged and Leave events.
private void resetDateTimePickerShowUpDown(object sender, EventArgs e)
{
    this.dateTimePicker1.ShowUpDown = true;
}

Upvotes: 0

rism
rism

Reputation: 12142

In a very general sense you are talking about binding to events. In this case you are interested in binding to both keyboard and mouse events.

So look up the reference material on keypress, keydown events and click events.

Within keypress events you can filter for the type of key or key combination that interests you in this the up / down keys.

Within the click event handler you would check to see which mouse button was held down ( left and right and so forth).

Once you have captured and filtered the event of interest, you can just trigger the drop down action on the date time picker. To display the calendar you can call InvokeOnClick on the datetimepicker.

EDIT: I'm assuming you're subclassing the control.

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

You have to send a windows message to the button or the control, it is not exposed by the control.

This article has a good example of simulating a press on the button.

Upvotes: 1

Related Questions