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

前端 未结 1 1337
感情败类
感情败类 2021-01-06 05:42

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

I just tried like this:

 datetimepicker1.Format = DateTimePickerForm         


        
相关标签:
1条回答
  • 2021-01-06 06:06

    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;
    }
    
    0 讨论(0)
提交回复
热议问题