当我单击清除按钮时,我想清除我的 datetimepicker 对象值。
我只是这样试过:
datetimepicker1.Format = DateTimePickerFormat.Short;
datetimepicker1.CustomFormat = " ";
是的,它清除了 datetimepicker 控件下的值。但问题是在清除值之后,我想在选择特定日期后选择日期,该日期不会显示到 datetimepicker 对象中。有什么问题?
最佳答案
这应该符合您的目的。逻辑是将日期重置为最小值,然后使用 ValueChanged
事件使用您已经尝试过的将其隐藏在显示中。
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;
}