How can I make a DateTimePicker display an empty string?

brasskazoo picture brasskazoo · May 11, 2009 · Viewed 180.1k times · Source

I would like to be able to display a DateTimePicker that has a default value of nothing, i.e. no date.

For example, I have a start date dtTaskStart and an end date dtTaskEnd for a task, but the end date is not known, and not populated initially.

I have specified a custom format of yyyy-MM-dd for both controls.

Setting the value to null, or an empty string at runtime causes an error, so how can I accomplish this?

I have considered using a checkbox to control the enabling of this field, but there is still the issue of displaying an initial value..

Edit:
Arguably a duplicate of the question DateTimePicker Null Value (.NET), but the solution I found for my problem is not a solution for that question, so I think it should remain here for others to find..

Answer

Sérgio Domingos picture Sérgio Domingos · Dec 9, 2011

Just set the property as follows:

When the user press "clear button" or "delete key" do

dtpData.CustomFormat = " "  'An empty SPACE
dtpData.Format = DateTimePickerFormat.Custom

On DateTimePicker1_ValueChanged event do

dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss"