glant glant - 2 months ago 7x Question

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

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 opens up. I am looking for something similar to DateSelected event in the Calender control

I have a class that derives from DateTimePicker which uses custom format with a spacechar string for to have empty field for DateTimePicker.

I found an article in stackoverflow with same issue but the solution doesnot work. I don't have enough points to place comments either (unable to find the link now :( )

As far as code this is what i have

Public Class CustomDP
Inherits System.Windows.Forms.DateTimePicker

Public Sub New()
CustomFormat = " "
Format = System.Windows.Forms.DateTimePickerFormat.Custom
End Sub

Protected Overrides Sub OnValueChanged(eventargs As System.EventArgs)
End Sub

Protected Overrides Sub OnCloseUp(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Short
End Sub

Protected Overrides Sub OnDropDown(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Custom
End Sub
End Class


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.