How can I make a DateTimePicker display an empty string?
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"
Obfuscating the value by using the CustomFormat
property, using checkbox cbEnableEndDate
as the 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
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 one guys!
In case anybody has an issue with setting datetimepicker control to blank during the form load event, and then show the current date as needed, here is an example:
MAKE SURE THAT CustomFormat = " "
has 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
When I want to display an empty date value I do this
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 an empty date value, but still allow the user to be able to change the date to something when they wish.
Keep in mind that sStrDate has already been validated as a valid date string.