.netdatetimepickernull

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


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


Solution

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