Click here to Skip to main content
15,850,446 members
Articles / Programming Languages / C#
Article

Nullable DateTimePicker

Rate me:
Please Sign up or sign in to vote.
4.84/5 (41 votes)
17 Nov 2003 416.4K   18.2K   54   60
A standard DateTimePicker control that enables users to enter null value. The fact that it's not intensively modified ensures that it has no potential errors.

Sample Image - Nullable_DateTimePicker.jpg

Introduction

I had do a research on a DateTimePicker that could enter null value and found a lot of solutions to it. But one major problem with those is that they behave quite different from the standard ones and may have many potential bugs, as a result it took time to test before using in a real application. I just found another simple solution that could solve this problem with a little modification, so it could promise no bugs :-).

Solution

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. That's all there's to it.

Because it's so simple, there's not much to say about it. Please refer to code for details.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseSimple is beautiful! Pin
ineedajobsoon4-Mar-16 3:54
ineedajobsoon4-Mar-16 3:54 
QuestionNullable DateTimePicker Pin
Member 1174358825-Jun-15 21:47
Member 1174358825-Jun-15 21:47 
QuestionKhông sử dụng được Custom Format Pin
Do Cao Tri13-Dec-14 6:25
Do Cao Tri13-Dec-14 6:25 
QuestionNot work anymore ? Pin
duonglei16-Jun-14 23:20
duonglei16-Jun-14 23:20 
QuestionData Binding Pin
wiggles30-May-14 6:50
wiggles30-May-14 6:50 
QuestionProblem with this control Pin
MarkRyan19812-Jan-14 0:53
MarkRyan19812-Jan-14 0:53 
QuestionNot able to download the Source Files Pin
hdng7631-Oct-12 6:00
hdng7631-Oct-12 6:00 
QuestionHow easy is that!! Pin
Woody10022-Sep-12 1:30
Woody10022-Sep-12 1:30 
QuestionJust what I needed Pin
gemini_dk20-Dec-11 5:43
gemini_dk20-Dec-11 5:43 
AnswerRe: Just what I needed Pin
jf6425-Jan-12 5:57
jf6425-Jan-12 5:57 
GeneralNo hour in sql database Pin
cyrilcp9-Nov-10 1:16
cyrilcp9-Nov-10 1:16 
GeneralValidation wrong value fix Pin
PREMSONBABY20-May-10 5:38
PREMSONBABY20-May-10 5:38 
GeneralRe: Validation wrong value fix Pin
oliwan30-Sep-10 13:16
oliwan30-Sep-10 13:16 
GeneralRe: Validation wrong value fix Pin
DeuceDude28-Oct-11 16:46
DeuceDude28-Oct-11 16:46 
Generalthank you for your help! Pin
wuhai08095-May-10 15:52
wuhai08095-May-10 15:52 
GeneralSet Nullable DateTimePicker default value [00/00/0000] Pin
tinhleduc26-Feb-10 1:27
tinhleduc26-Feb-10 1:27 
GeneralUpdate Pin
Romain TAILLANDIER21-Sep-09 23:37
Romain TAILLANDIER21-Sep-09 23:37 
Hello,

I use your code, because it is elegant, simple and efficient, as Phil Raevsky said.
I have used all remarks from the forum and found some new upgrades.

public class NullableDateTimePicker : System.Windows.Forms.DateTimePicker
{
    DateTimePickerFormat _Format;
    string _CustomFormat;
    private DateTime? _ValueNullable;

    public NullableDateTimePicker()
        : base()
    {
        _Format = base.Format;
        _CustomFormat = base.CustomFormat;
    }

    // overrides of this two property 
    // because in case of modifying Format or CustomFormat at runtime, saved values become obsolete, and could be restored

    public new DateTimePickerFormat Format
    {
        get { return base.Format; }
        set { this._Format = value; base.Format = value; }
    }
    public new string CustomFormat
    {
       get { return base.CustomFormat; }
       set { this._CustomFormat = value; base.CustomFormat = value; }
    }

    // using the nullable Datetime from alc_aardvark
    public DateTime? ValueNullable
    {
        get 
        { 
            // May be have to test this
            // if(_ValueNullable.HasValue && (_ValueNullable.Value <= this.MinDate || _ValueNullable.Value >= this.MaxDate))
            //     // do something
            return _ValueNullable; 
        }
        set
        {
            _ValueNullable = value;
            // Be carefull of limits, that cause ArgumentOutOfRangeExceptions
            // Limits are not from DateTime, but from this !
            // In this case Over range values, i do as if value was null (for printing)
            if(!_ValueNullable.HasValue || value <= this.MinDate || value >= this.MaxDate) 
            {
                // show nothing in datetime picker
                base.Format = DateTimePickerFormat.Custom;
                base.CustomFormat = " ";
                base.OnValueChanged(EventArgs.Empty);
            }
            else
            {
                // restore printing.
                base.Format = _Format;
                base.CustomFormat = _CustomFormat;
                base.Value = _ValueNullable.Value;
            }
        }
    }

    // Override this property to be sure ValueNullable is used.
    public new DateTime Value
    {
        get
        {
            if(!_ValueNullable.HasValue) // if( value == null)
               return this.MinDate; // or DateTime.MinValue
            return ValueNullable.Value;
        }
        set
        {
            this.ValueNullable = value;
        }
    }

    protected override void OnKeyDown(KeyEventArgs e) 
    { 
        ... //same as original version
    }
    protected override void OnCloseUp(EventArgs eventargs)
    { 
        ... //same as original version
    }
       
}


Hope that could help.

Romain TAILLANDIER
www.romaintaillandier.blogspot.com
romaintaillandier.free.fr
www.maintag.fr

GeneralThis is a very insightful approach to the problem Pin
Phil Raevsky28-Aug-09 17:01
Phil Raevsky28-Aug-09 17:01 
GeneralThis one dispaly pink if null date (VB.NET) Pin
Larry Rouse28-Aug-09 12:32
Larry Rouse28-Aug-09 12:32 
GeneralThis one works! (VB 2008) [modified] Pin
Jeromeyers10-Feb-09 13:55
Jeromeyers10-Feb-09 13:55 
GeneralRe: This one works! (VB 2008) Pin
EMYNA28-Oct-09 13:40
EMYNA28-Oct-09 13:40 
GeneralRe: This one works! (VB 2008) [modified] Pin
Chris Wineinger13-Feb-13 9:31
Chris Wineinger13-Feb-13 9:31 
GeneralUse the Nullable Datetime for a more updated solution. Pin
chstngs7-Oct-08 6:39
chstngs7-Oct-08 6:39 
GeneralIt's really great! Thank you very much!! Pin
wxpwu15-Jun-08 3:48
wxpwu15-Jun-08 3:48 
GeneralSmall correction Pin
Maxxx33316-Nov-06 2:32
Maxxx33316-Nov-06 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.