Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear datetimepicker value in windows application using c#?

Tags:

c#

winforms

I want to clear my datetimepicker object value when i click on the clear button.

I just tried like this:

 datetimepicker1.Format = DateTimePickerFormat.Short;
 datetimepicker1.CustomFormat = " "; 

Yes,it is cleared the value under the datetimepicker control. But the problem is after cleared the value then, I want to select the date after selection of the particular date, that date will not be displayed into datetimepicker object. What's the issue?

like image 603
Nag Avatar asked Dec 14 '22 10:12

Nag


1 Answers

This should meet your purpose. Logic is to reset date to minimum and then use ValueChanged event to hide it in display using what you already tried.

private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if (dateTimePicker1.Value == DateTimePicker.MinimumDateTime)
    {
        dateTimePicker1.Value = DateTime.Now; // This is required in order to show current month/year when user reopens the date popup.
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.CustomFormat = " ";
    }
    else
    {
        dateTimePicker1.Format = DateTimePickerFormat.Short;
    }
}

private void Clear_Click(object sender, EventArgs e)
{
    dateTimePicker1.Value = DateTimePicker.MinimumDateTime;
}
like image 107
Nikhil Vartak Avatar answered Dec 17 '22 01:12

Nikhil Vartak