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

回眸只為那壹抹淺笑 提交于 2019-11-29 15:51:10

There is already at least 1 DateTimePicker control on CodeProject which allows for a Nullable Date. But there is a simpler method than that.

In design, set the ShowCheckBox to True. Then when the form loads or you are resetting fields, set Checked to False. This makes the DTP look disabled which is a little unfortunate, but when the user opens the DTP, the Checkbox is automatically checked. So, all you need to do to see if they picked a date, is to evaluate the Checked property. The disabled appearance ends up indicating that the value doesnt really count, if they should happen to uncheck it after selecting a valid date.

If you use the smaller version (ShowUpDown), the control is disabled until they first click the checkbox, then select a field to spin thru. You will still get a value changed event for each change of each field, but thats how it is supposed to work. The control always has a valid date value so you dont have to check against Nothing etc.

Another way to be sure a date is picked is to "mirror" the DTP value in code (which is likely what you'd end up doing in a subclassed control). This is only a partial solution because the control will set a date value if they flip thru months. Its just how the control works.

Public Class Form1

    ' "mirror" dtp value vars
    Private myFromDate As Date = Date.MinValue
    Private myToDate As Date = Date.MaxValue

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        ResetDates()

    End Sub

    Private Sub ResetDates()
        ' reset the dates for repeated entries,
        ' ValueChanged event will fire
        myFromDate = Date.MinValue
        myToDate = Date.MaxValue

        ' the button only enables for valid dates
        Button1.Enabled = False
    End Sub

    Private Sub CheckDates()

        ' signal that all is well
        Button1.Enabled = ((myFromDate <> Date.MinValue) AndAlso
                          (myToDate <> Date.MaxValue) AndAlso
                          (myToDate > myFromDate))

        ' debugging while a DTP is open can be...problematic
        Console.WriteLine("from {0} to {1}  {2}", 
                       myFromDate.ToString, myToDate.ToString, 
                       (myToDate > myFromDate).ToString)

    End Sub

    Private Sub dtp_ValueChanged(sender As Object, e As EventArgs) _
                        Handles dtpFrom.ValueChanged, dtpTo.ValueChanged

        Dim dtp As DateTimePicker = CType(sender, DateTimePicker)

        Select Case dtp.Name
            Case "dtpFrom"
                If dtp.Value = dtp.MinDate Then
                    myFromDate = Date.MinValue      ' used when Resetting
                Else
                    ' myFromDate <> Date.Min acts as a flag
                    myFromDate = dtp.Value          
                End If

            Case "dtpTo"
                If dtp.Value = dtp.MaxDate Then
                    myToDate = Date.MinValue        ' Resetting
                Else
                    myToDate = dtp.Value            ' user pick
                End If
        End Select

        CheckDates()
    End Sub

Its a bit more code than the ShowCheckbox method but something like what you would need for subclassing. If you subclass the DTP, you'd likely raise a new event from this ValueChanged event when the mirror variable <> Date.Min|Max and CheckDates would not be needed.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!