c# - 如何使用 C# 清除 Windows 应用程序中的 datetimepicker 值?

标签 c# winforms

我想在单击清除按钮时清除我的 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;
}

关于c# - 如何使用 C# 清除 Windows 应用程序中的 datetimepicker 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772295/

相关文章:

c# - SELECT 似乎阻止了 UPDATE 语句

'new'方法中的C#递归

c# - 如何聚焦模式对话框单击 C# 中的通知图标?

c# - 使用现有控件向窗体添加选项卡控件

c# - .net core 3.0,IClassFixture 问题, "unresolved constructor arguments: ITestOutputHelper output"

c# - 尝试在带有 Visual Studio 2010 的 C# 中创建自动版本控制系统

c# - 使用 RIGHT JOINT 将两个表连接到一个数据 GridView 中

c# - 如何在 C# 中创建自定义 DataGrid View - Winforms 应用程序

c# - 如何将值传递给winforms c#中的用户控件

c# - 在 BackGroundWorker 中运行方法并显示 ProgressBar