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 dtTaskStar
The basic concept is the same told by others. But its 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;
the value changed 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;
}
}
When unchecked
When checked
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