Question

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

Was it helpful?

Solution

Here is an interesting article on CodeProject. The author has overridden the Value property to accept Null value as DateTime.MinValue, while maintaining the validation of MinValue and MaxValue of the standard control. That's all there's to it.

OTHER TIPS

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

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

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

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

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;
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top