Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
GWLlosa Avatar asked Nov 12 '08 15:11

GWLlosa


People also ask

How can I make a DateTimePicker display an empty string?

Set datetimepicker FORMAT property to custom. set CUSTOM FORMAT property to empty string " ". set its TAG to 0 by default.

How do you make a DatePicker blank?

DatePicker uses valueDefault property. It is by default set to new Date() . You just need to set property valueDefault={null} and the DatePicker will be blank.

What is the purpose of VB net DateTimePicker control?

The DateTimePicker control allows selecting a date and time by editing the displayed values in the control. If you click the arrow in the DateTimePicker control, it displays a month calendar, like a combo box control. The user can make selection by clicking the required date.

What is DateTimePicker in Visual Studio?

Represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.


3 Answers

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

like image 91
Jan Obrestad Avatar answered Oct 20 '22 16:10

Jan Obrestad


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;
        }
    }
}
like image 42
splattne Avatar answered Oct 20 '22 14:10

splattne


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

like image 4
Serhat Ozgel Avatar answered Oct 20 '22 14:10

Serhat Ozgel