Make DateTimePicker work as TimePicker only in WinForms

C#Winforms

C# Problem Overview


How to restrict DateTimePicker to select the time only? I don't know how to disable calendar control which drops when you press the button at the right of DateTimePicker.

C# Solutions


Solution 1 - C#

A snippet out of the MSDN:

> 'The following code sample shows how > to create a DateTimePicker that > enables users to choose a time only.'

timePicker = new DateTimePicker();
timePicker.Format = DateTimePickerFormat.Time;
timePicker.ShowUpDown = true;

And for anyone who wants to know what it looks like:

enter image description here

Solution 2 - C#

...or alternatively if you only want to show a portion of the time value use "Custom":

timePicker = new DateTimePicker();
timePicker.Format = DateTimePickerFormat.Custom;
timePicker.CustomFormat = "HH:mm"; // Only use hours and minutes
timePicker.ShowUpDown = true;

Solution 3 - C#

You want to set its 'Format' property to be time and add a spin button control to it:

yourDateTimeControl.Format = DateTimePickerFormat.Time;
yourDateTimeControl.ShowUpDown = true;

Solution 4 - C#

If you want to do it from properties, you can do this by setting the Format property of DateTimePicker to DateTimePickerFormat.Time and ShowUpDown property to true. Also, customFormat can be set in properties.

Solution 5 - C#

The best way to do this is this:

datetimepicker.Format = DatetimePickerFormat.Custom;
datetimepicker.CustomFormat = "HH:mm tt";
datetimepicker.ShowUpDowm = true;

Solution 6 - C#

Add below event to DateTimePicker

Private Sub DateTimePicker1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DateTimePicker1.KeyPress
    e.Handled = True
End Sub

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlablablasterView Question on Stackoverflow
Solution 1 - C#PeterView Answer on Stackoverflow
Solution 2 - C#Sverrir SigmundarsonView Answer on Stackoverflow
Solution 3 - C#SeeSharpView Answer on Stackoverflow
Solution 4 - C#sguptaView Answer on Stackoverflow
Solution 5 - C#Randy Peña JimenezView Answer on Stackoverflow
Solution 6 - C#Nagaraju MenganiView Answer on Stackoverflow