24

null更改 .NET DateTimePicker 控件以允许用户输入值的最简单和最可靠的方法是什么?

4

6 回答 6

47

您无需修改​​它即可执行此操作。

DateTimePickerin .net 实际上有一个内置的复选框。

ShowCheckBox属性设置为true

然后您可以使用该Checked属性来查看用户是否输入了值。

http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.showcheckbox(VS.80).aspx

于 2009-11-19T12:32:48.187 回答
6

这是此 CodeProject 文章中有关创建Nullable DateTimePicker的方法。

我已经重写了Value属性以接受Nullvalue as DateTime.MinValue,同时保持对标准控件的验证MinValueMaxValue对标准控件的验证。

这是文章中自定义类组件的一个版本

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;
        }
    }
}
于 2008-11-12T15:56:47.480 回答
4

放置一个额外的复选框,标记为“启用通知”,启用/禁用 DateTimePicker。

于 2008-11-12T15:50:18.103 回答
2

Tri 的解决方案对我来说并没有完全解决,所以 Grazioli 先生认为他做了一些事情: http: //www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c9645

于 2008-12-09T13:54:21.877 回答
0

我发布了很长的解决方案,在代码注释中对这个控件的特殊问题有一些发现:

日期时间选择器空值

于 2011-12-23T11:33:59.053 回答
0

ShowCheckBox属性设置为true

然后您可以使用 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;
    }
}
于 2015-10-09T06:26:59.043 回答