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..
This question is related to
.net
winforms
datetimepicker
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;
}
}
Better to use text box for calling/displaying date and while saving use DateTimePicker. Make visible property true or false as per requirement.
For eg : During form load make Load date in Textbox and make DTPIcker invisible and while adding vice versa
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!
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"
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.
Listen , Make Following changes in your code if you want to show empty datetimepicker and get null when no date is selected by user, else save date.
if user will interact with datetimepicker its VALUECHANGED event will be called and there set its TAG property to 1.
Now when saving, check if its TAG is zero, then save NULL date else if TAG is 1 then pick and save Datetime picker value.
It Works like a charm.
Now if you want its value be changed back to empty by user interaction, then add checkbox and show text "Clear" with this checkbox. if user wants to clear date, simply again set its CUSTOM FORMAT property to empty string " ", and set its TAG back to 0. Thats it..
Source: Stackoverflow.com