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)
MyBase.OnValueChanged(eventargs)
End Sub
Protected Overrides Sub OnCloseUp(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Short
MyBase.OnCloseUp(eventargs)
End Sub
Protected Overrides Sub OnDropDown(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Custom
MyBase.OnDropDown(eventargs)
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, setChecked
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 theChecked
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 againstNothing
etc.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:
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
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.
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.