Make DateTimePicker work as TimePicker only in WinForms

前端 未结 6 1707
南笙
南笙 2020-12-08 06:01

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

相关标签:
6条回答
  • 2020-12-08 06:28

    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;
    
    0 讨论(0)
  • 2020-12-08 06:34

    ...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;
    
    0 讨论(0)
  • 2020-12-08 06:34

    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.

    0 讨论(0)
  • 2020-12-08 06:35

    Add below event to DateTimePicker

    Private Sub DateTimePicker1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DateTimePicker1.KeyPress
        e.Handled = True
    End Sub
    
    0 讨论(0)
  • 2020-12-08 06:39

    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;
    
    0 讨论(0)
  • 2020-12-08 06:49

    The best way to do this is this:

    datetimepicker.Format = DatetimePickerFormat.Custom;
    datetimepicker.CustomFormat = "HH:mm tt";
    datetimepicker.ShowUpDowm = true;
    
    0 讨论(0)
提交回复
热议问题