Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to show calendar in the DateTimePicker when user clicks DropDown button

Tags:

c#

winforms

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??

like image 525
user2273044 Avatar asked Apr 12 '13 05:04

user2273044


People also ask

Which property must be changed in the DateTimePicker control to only display the time?

To display the time with the DateTimePicker control Set the ShowUpDown property for the DateTimePicker to true .

What is a date time picker?

The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format. The DateTimePicker control makes it easy to work with dates and times because it handles a lot of the data validation automatically.

Which control is used as a DatePicker?

The DatePicker control allows the user to select a date by either typing it into a text field or by using a drop-down Calendar control.

What is date/time picker in Visual Studio?

The DateTimePicker control allows selecting a date and time by editing the displayed values in the control. If you click the arrow in the DateTimePicker control, it displays a month calendar, like a combo box control. The user can make selection by clicking the required date.


1 Answers

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.

like image 185
Cody Gray Avatar answered Sep 28 '22 05:09

Cody Gray