How to make DateTimePicker display an empty string?

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

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

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

Setting it to null or an empty string at runtime causes an error, so how can I do this?

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

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

+52
winforms datetimepicker
May 11 '09 at
source share
6 answers

Obfuscate the value using the CustomFormat property, using the cbEnableEndDate flag as a flag to indicate whether other code should ignore the value:

 If dateTaskEnd > Date.FromOADate(0) Then dtTaskEnd.Format = DateTimePickerFormat.Custom dtTaskEnd.CustomFormat = "yyyy-MM-dd" dtTaskEnd.Value = dateTaskEnd dtTaskEnd.Enabled = True cbEnableEndDate.Checked = True Else dtTaskEnd.Format = DateTimePickerFormat.Custom dtTaskEnd.CustomFormat = " " dtTaskEnd.Value = Date.FromOADate(0) dtTaskEnd.Enabled = False cbEnableEndDate.Checked = False End If 
+26
May 11 '09 at 1:09 a.m.
source share

Just set the property as follows:

When the user clicks the Clear or Delete Key button, do

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

In a DateTimePicker1_ValueChanged event

 dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss" 
+40
Dec 09 '11 at 16:23
source share

this worked for me for c #

 if (enableEndDateCheckBox.Checked == true) { endDateDateTimePicker.Enabled = true; endDateDateTimePicker.Format = DateTimePickerFormat.Short; } else { endDateDateTimePicker.Enabled = false; endDateDateTimePicker.Format = DateTimePickerFormat.Custom; endDateDateTimePicker.CustomFormat = " "; } 

Nice guys!

+19
Jun 18 2018-10-18
source share

In case someone had a problem with setting the datetimepicker control to an empty value during the form loading event, and then, if necessary, show the current date, here is an example:

MAKE SURE CustomFormat = " " has the same number of spaces (at least one space) in both methods

 Private Sub setDateTimePickerBlank(ByVal dateTimePicker As DateTimePicker) dateTimePicker.Visible = True dateTimePicker.Format = DateTimePickerFormat.Custom dateTimePicker.CustomFormat = " " End Sub Private Sub dateTimePicker_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles dateTimePicker.MouseHover Dim dateTimePicker As DateTimePicker = CType(sender, DateTimePicker) If dateTimePicker.Text = " " Then dateTimePicker.Text = Format(DateTime.Now, "MM/dd/yyyy") End If End Sub 
+5
Feb 05 '14 at
source share

When I want to show an empty date value, I do it

  if (sStrDate != "") { dateCreated.Value = DateTime.Parse(sStrDate); } else { dateCreated.CustomFormat = " "; dateCreated.Format = DateTimePickerFormat.Custom; } 

Then, when the user clicks on the control, I have this:

  private void dateControl_MouseDown(object sender, MouseEventArgs e) { ((DateTimePicker)sender).Format = DateTimePickerFormat.Long; } 

This allows you to display and use the empty date value, but allows the user to change the date whenever he wants.

Keep in mind that sStrDate is already checked as a valid date string.

+4
Jan 09 '16 at 1:41
source share

The basic concept is the same as the others. But it is easier to implement this way when you have multiple dateTimePicker.

 dateTimePicker1.Value = DateTime.Now; dateTimePicker1.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged); dateTimePicker1.ShowCheckBox=true; dateTimePicker1.Checked=false; dateTimePicker2.Value = DateTime.Now; dateTimePicker2.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged); dateTimePicker2.ShowCheckBox=true; dateTimePicker2.Checked=false; 

value of the modified event function

  void Dtp_ValueChanged(object sender, EventArgs e) { if(((DateTimePicker)sender).ShowCheckBox==true) { if(((DateTimePicker)sender).Checked==false) { ((DateTimePicker)sender).CustomFormat = " "; ((DateTimePicker)sender).Format = DateTimePickerFormat.Custom; } else { ((DateTimePicker)sender).Format = DateTimePickerFormat.Short; } } else { ((DateTimePicker)sender).Format = DateTimePickerFormat.Short; } } 

If not installed enter image description here

When checking enter image description here

0
May 16 '17 at 4:48 p.m.
source share



All Articles