How to alter a .NET DateTimePicker control to allow enter null values?

扶醉桌前 提交于 2019-11-26 14:27:16

问题


What's the easiest and most robust way of altering the .NET DateTimePicker control, to allow users to enter null values?


回答1:


Here's an approach from this CodeProject article on creating a Nullable DateTimePicker.

I have overridden the Value property to accept Null value as DateTime.MinValue, while maintaining the validation of MinValue and MaxValue of the standard control.

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;
        }
    }
}



回答2:


You don't need to modify it to do this.

The DateTimePicker in .net actually has a checkbox built-in.

Set the ShowCheckBox property to true.

Then you can use the Checked property to see if the user has entered a value.

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




回答3:


Placing an additional checkbox labeled something like "enable notification" that enables / disables the DateTimePicker.




回答4:


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




回答5:


I posted my long way around solution, which has some findings in the code comments about the peculiar issues with this control:

DateTime Picker null value




回答6:


Set the ShowCheckBox property to true.

Then you can use the Checked property as follows:

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;
    }
}


来源:https://stackoverflow.com/questions/284364/how-to-alter-a-net-datetimepicker-control-to-allow-enter-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!