如何更改 .NET DateTimePicker 控件以允许输入空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/284364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to alter a .NET DateTimePicker control to allow enter null values?
提问by GWLlosa
What's the easiest and most robust way of altering the .NET DateTimePicker control, to allow users to enter nullvalues?
更改 .NET DateTimePicker 控件以允许用户输入null值的最简单和最可靠的方法是什么?
采纳答案by splattne
Here's an approach from this CodeProject article on creating a Nullable DateTimePicker.
这是 CodeProject 文章中关于创建Nullable DateTimePicker的一种方法。
I have overridden the
Valueproperty to acceptNullvalue asDateTime.MinValue, while maintaining the validation ofMinValueandMaxValueof the standard control.
我已经覆盖了
Value接受属性Null值DateTime.MinValue,同时保持的验证MinValue和MaxValue标准的控制。
Here's a version of the custom class component from the article
这是文章中自定义类组件的一个版本
public class NullableDateTimePicker : System.Windows.Forms.DateTimePicker
{
private DateTimePickerFormat originalFormat = DateTimePickerFormat.Short;
private string originalCustomFormat;
private bool isNull;
public new DateTime Value
{
get => isNull ? DateTime.MinValue : base.Value;
set
{
// incoming value is set to min date
if (value == DateTime.MinValue)
{
// if set to min and not previously null, preserve original formatting
if (!isNull)
{
originalFormat = this.Format;
originalCustomFormat = this.CustomFormat;
isNull = true;
}
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
else // incoming value is real date
{
// if set to real date and previously null, restore original formatting
if (isNull)
{
this.Format = originalFormat;
this.CustomFormat = originalCustomFormat;
isNull = false;
}
base.Value = value;
}
}
}
protected override void OnCloseUp(EventArgs eventargs)
{
// on keyboard close, restore format
if (Control.MouseButtons == MouseButtons.None)
{
if (isNull)
{
this.Format = originalFormat;
this.CustomFormat = originalCustomFormat;
isNull = false;
}
}
base.OnCloseUp(eventargs);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
// on delete key press, set to min value (null)
if (e.KeyCode == Keys.Delete)
{
this.Value = DateTime.MinValue;
}
}
}
回答by Jan Obrestad
You don't need to modify it to do this.
您无需修改它即可执行此操作。
The DateTimePickerin .net actually has a checkbox built-in.
在DateTimePicker.NET中实际上有内置的复选框。
Set the ShowCheckBoxproperty to true.
将ShowCheckBox属性设置为true。
Then you can use the Checkedproperty to see if the user has entered a value.
然后您可以使用该Checked属性查看用户是否输入了值。
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.showcheckbox(VS.80).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.showcheckbox(VS.80).aspx
回答by Serhat Ozgel
Placing an additional checkbox labeled something like "enable notification" that enables / disables the DateTimePicker.
放置一个标记为“启用通知”之类的附加复选框,以启用/禁用 DateTimePicker。
回答by Serhat Ozgel
Tri's solution did not quite cut it for me and so thought Mr. Grazioli and he did something about it: http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c9645
Tri 的解决方案对我来说并没有完全解决,所以 Grazioli 先生认为他做了一些事情:http: //www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c9645
回答by quickdraw
I posted my long way around solution, which has some findings in the code comments about the peculiar issues with this control:
我发布了我的解决方案,在代码注释中有一些关于此控件的特殊问题的发现:
回答by Eduard
Set the ShowCheckBoxproperty to true.
将ShowCheckBox属性设置为true。
Then you can use the Checked property as follows:
然后您可以按如下方式使用 Checked 属性:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTimePicker thisDateTimePicker = (DateTimePicker)sender;
if (thisDateTimePicker.Checked == false)
{
thisDateTimePicker.CustomFormat = @" "; //space
thisDateTimePicker.Format = DateTimePickerFormat.Custom;
}
else
{
thisDateTimePicker.Format = DateTimePickerFormat.Short;
}
}

