5

In the DateTimePicker when user clicks DropDown button, a calendar gets displayed. I don't want to show calendar on the DropDown event.

Is there some way to overwrite this event and to show custom form when user clicks DropDown button??

Haris
  • 9
  • 2
user2273044
  • 161
  • 2
  • 17
  • 1
    Welcome to Stackoverflow. Did you tried _anything_ to solve your problem? Show your effort. Please read [FAQ] and [ask] – Soner Gönül Apr 12 '13 at 05:49

1 Answers1

5

I don't want to show calendar on the DropDown event.

You can prevent the calendar from being displayed by setting the control's ShowUpDown property to true. Instead of the drop-down calendar, it will instead show a spin button to the side of the DateTimePIcker control that allows the user to select the desired date/time.

Is there some way to overwrite this event and to show custom form when user clicks DropDown button??

Well, yeah, I guess so. You can handle the DropDown event, which gets raised when the control shows the drop-down calendar. In this event handler, you could display whatever form you wanted. The only problem is that the drop-down calendar is still going to be shown as normal. I don't know how you prevent that from happening.

You can, however, hack around it by forcing the drop-down calendar closed again. To do that, P/Invoke the SendMessage function and send the control the DTM_CLOSEMONTHCAL message.

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

private const uint DTM_FIRST         = 0x1000;
private const uint DTM_CLOSEMONTHCAL = DTM_FIRST + 13; 

public static void CloseCalendar(this DateTimePicker dtp)
{
    SendMessage(dtp.Handle, DTM_CLOSEMONTHCAL, IntPtr.Zero, IntPtr.Zero);
}

But I can't imagine the reason you would want to do this. If you don't want a calendar or a spin-button control, then you don't want a DateTimePicker. Just use a regular combo box or a drop-down button, then you can display whatever form you want when it is clicked.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • `the drop-down calendar is still going to be shown as normal. I don't know how you prevent that from happening.` A really dirty way is place a button over the top of the Calendar DropDown button... hey presto! – Jeremy Thompson Apr 12 '13 at 06:13
  • Dirty enough that it won't always work. If the DateTimePicker control has the focus (and it will need to in order for the user to, uhm, use it), then `F4` will drop down the calendar. – Cody Gray - on strike Apr 12 '13 at 06:16
  • its supposed to be funny, suppress the `F4` keypress. Actually there ARE times when you need to do this: http://stackoverflow.com/questions/10526118/bug-cant-choose-dates-on-a-datepicker-that-fall-outside-a-floating-vsto-add-in – Jeremy Thompson Apr 12 '13 at 06:20
  • Thanks for response. But calendar still appears after clicking the dropdown button. The reason of this hack I'm not satisfied with the current DateTimePicker control. And I'm trying to open my custom form under DateTimePicker instead of standard calendar. I could create my own UserControl and place button over the top calendar button, but this is dirty way – user2273044 Apr 12 '13 at 07:12