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
.
Asked
Active
Viewed 1.3e+01k times
91

David Ferenczy Rogožan
- 23,966
- 9
- 79
- 68

Blablablaster
- 3,238
- 3
- 31
- 33
6 Answers
160
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:
-
11Ha, the solution was simple: just set datetimepicker's ShowUpDown property to true and set Format to DateTimePickerFormat.Time. Only setting Format to DateTimePickerFormat.Time is not enough. – Blablablaster Apr 08 '11 at 10:24
45
...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;

Sverrir Sigmundarson
- 2,453
- 31
- 27
-
7If you use this route and you need to specify AM or PM you can add `tt` to the string. Full list of format options here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.customformat(v=vs.110).aspx – AdamMc331 Dec 11 '15 at 20:11
14
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;

SeeSharp
- 2,760
- 16
- 15
2
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.

David Ferenczy Rogožan
- 23,966
- 9
- 79
- 68

sgupta
- 535
- 4
- 8
2
The best way to do this is this:
datetimepicker.Format = DatetimePickerFormat.Custom;
datetimepicker.CustomFormat = "HH:mm tt";
datetimepicker.ShowUpDowm = true;

Randy Peña Jimenez
- 132
- 2
- 3
-
1I like how you added the "tt" to custom format so we can choose AM or PM – Jamisco May 16 '18 at 03:34
-2
Add below event to DateTimePicker
Private Sub DateTimePicker1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DateTimePicker1.KeyPress
e.Handled = True
End Sub

Nagaraju Mengani
- 207
- 3
- 2
-
4This language definitely doesn't look like C#. Also, you should explain what that code does. How it disables changes of date, but preserves changes of time. – David Ferenczy Rogožan Apr 08 '16 at 15:39