0

I have an issue with a DateTimePicker component which doesn't work as I want to. First, here is the wanted behavior of the DateTimePickers in the form :

When the user arrives on the screen, the dates are empty. Then he clicks on one of them, which activates it and displays the current date. The user can now type in a date, or use the calendar picker to select one.

For the date to be empty initially, I use a custom format and set it to ' ' (space). The date is initialized at the form loading with the current date, but not visible thanks to this custom format. The 'Checked' property is set to false.

Then when the user clicks in it, it sets the format to 'Short', and displays the date. The 'Checked' property is set to true, too.

There is the function which activates or deactivates a DateTimePicker, depending of the boolean parameter :

Private Sub Activation_date(dtp As DateTimePicker, activation As Boolean)
    If activation And dtp.Checked = False Then
        dtp.Format = DateTimePickerFormat.Short
        dtp.Checked = True
    ElseIf Not activation And dtp.Checked = True Then
        dtp.Format = DateTimePickerFormat.Custom
        dtp.CustomFormat = " "
        dtp.Checked = False
    End If
End Sub

This function is called on the Click event as well as on the DropDown event, to show the date. They both consist of this function call :

Activation_date(sender, True)

Here is the issue : When I click in the DateTimePicker field, the current date appears properly. When I click on the dropdown button to open the calendar, the date shows too. But I cannot select a date by clicking in the calendar : nothing happens, the date field still contains the initialized date and the calendar stays open instead of putting the date in the field :

Calendar open.

I have to close it, open it a second time and this time, it works.

Now, I guess it's caused by the format change when I click on the dropdown, but I cannot find why it acts that way and doesn't let me select a date directly, without having to close and reopen the calendar. As I see it, when opening the calendar, it should display the date in the field, and let me pick a date, closing the calendar.

Note that when I click in the date field first, it makes the date appear, and then when I open the dropdown picker, is works correcly, probably because the date format is already set.

Please let me know if you need for information or code. Thanks.

asmoth
  • 98
  • 2
  • 9
  • Sounds like article. Summarizing the problem is good. Maybe [this](https://stackoverflow.com/questions/846395/how-can-i-make-a-datetimepicker-display-an-empty-string) and [this](https://stackoverflow.com/questions/5947726/set-datetimepicker-value-to-be-null) and [this](https://stackoverflow.com/questions/33772295/how-to-clear-datetimepicker-value-in-windows-application-using-c) ....etc. ? –  Feb 19 '20 at 18:57
  • Thank you, yeah that's what I do to hide the date : using a custom format (" ") and set it back when I want the date to be displayed. But that doesn't help me with my issue regarding the dropdown. – asmoth Feb 20 '20 at 09:18

0 Answers0