how to identity if user clicked on a date in DateTimePicker calendar

前端 未结 4 1938
心在旅途
心在旅途 2020-12-21 17:55

I don\'t want the date to change when user clicks out of the calender without clicking on a date. The problem is One Date is always selected when the datepickercalendar open

4条回答
  •  醉梦人生
    2020-12-21 18:12

    An old thread but maybe someone finds this useful... If you're looking for DTP that opens "blank" and selects date only If user selects It, then this is (in my opinion) best and simplest option:

    Private Sub Form_Load(sender As Object, e As EventArgs) Handles Form.Load
    
            DTP_Example.Format = DateTimePickerFormat.Custom
            DTP_Example.CustomFormat = " "
    
    End Sub
    
     Private Sub DTP_Example_ValueChanged(sender As Object, e As EventArgs) Handles DTP_Example.ValueChanged
    
            DTP_Example.Format = DateTimePickerFormat.Long
    
     End Sub
    
     Private Sub DTP_Example_KeyDown(sender As Object, e As KeyEventArgs) Handles DTP_Example.KeyDown
    
            If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
                DTP_Example.Value = Now
                DTP_Example.Format = DateTimePickerFormat.Custom
                DTP_Example.CustomFormat = " "
    
            End If
    
        End Sub
    

    This way you have DTP "blank" on open, and displays date If you select some. Also you can delete dates with "Backspace" or "Delete" keys and even select same date as before, which makes DTP simmilar behavior like combobox/textbox. Same "Backspace" behavior as in textbox/combobox can be achieved too, with a little more coding (using another Textbox to manually enter Dates), but in my opinion this is good enough. Cheers

提交回复
热议问题