Click here to Skip to main content
15,905,875 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 418.5K   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

 
GeneralThis one works! (VB 2008) [modified] Pin
Jeromeyers10-Feb-09 12:55
Jeromeyers10-Feb-09 12:55 
GeneralRe: This one works! (VB 2008) Pin
EMYNA28-Oct-09 12:40
EMYNA28-Oct-09 12:40 
GeneralRe: This one works! (VB 2008) [modified] Pin
Chris Wineinger13-Feb-13 8:31
Chris Wineinger13-Feb-13 8:31 
GeneralUse the Nullable Datetime for a more updated solution. Pin
chstngs7-Oct-08 5:39
chstngs7-Oct-08 5:39 
GeneralIt's really great! Thank you very much!! Pin
wxpwu15-Jun-08 2:48
wxpwu15-Jun-08 2:48 
GeneralSmall correction Pin
Maxxx33316-Nov-06 1:32
Maxxx33316-Nov-06 1:32 
GeneralRe: Small correction - an enhancement to that correction Pin
ccbt9-Sep-09 19:27
ccbt9-Sep-09 19:27 
GeneralRe: Small correction - an enhancement to that correction BugFixing =) [modified] Pin
Leonunicorn4-Dec-09 1:45
Leonunicorn4-Dec-09 1:45 
Hi there,

I have fixed some bugs and...

extended it to...

- use two digits
- international support
- using numpad

bugs fixed:
- using Zero "0" numeric exception
- always got april
- can't use two digits
- null value exception


protected override void OnKeyDown(KeyEventArgs e)
 {
     base.OnKeyDown(e);
     if (e.KeyCode == Keys.Delete)
     {
         this.Value = DateTime.MinValue;     //<--- null does not work, so there is the origin value
     }
     else if ((this.Value == DateTime.MinValue || this.Value == null)
         && (e.KeyCode == Keys.Space || (Char.IsNumber((char)e.KeyValue) && e.KeyValue != 48)    //<-- "0" correction
         || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right
         || e.KeyCode == Keys.Left
         || (e.KeyValue >= 97 && e.KeyValue <= 105) //<-- NumPad1 - NumPad9
         ))
     {

         int typedDigit = 1;
         if (e.KeyValue >= 97 && e.KeyValue <= 105)
         {//<-- NumPad1 - NumPad9 must be calculated to numeric KeyValues
             typedDigit = int.Parse(((char)e.KeyValue - 46).ToString());
         }
         else
         {
             typedDigit = int.Parse(((char)e.KeyValue).ToString());
         }


         DateTime newDate = DateTime.Today;
         if (Char.IsNumber((char)e.KeyValue))
         {
             newDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, typedDigit);    //<-- this allows international format of the datetimepicker
         }
         this.Value = newDate;
         SendKeys.Send(typedDigit.ToString()); //<-- this allows typing two digits at once without typing it again as example "12" or "27"
     }


 }


modified on Friday, December 4, 2009 9:02 AM

GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 [modified] Pin
kipotlov11-Dec-09 6:14
kipotlov11-Dec-09 6:14 
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 Pin
Leonunicorn15-Dec-09 4:57
Leonunicorn15-Dec-09 4:57 
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 Pin
Leonunicorn4-Jan-10 3:42
Leonunicorn4-Jan-10 3:42 
GeneralRe: Small correction - an enhancement to that correction BugFixing =) [modified] Pin
Member 102369739-Feb-15 20:49
Member 102369739-Feb-15 20:49 
QuestionNull Button? Pin
Kysonel28-Mar-06 9:48
Kysonel28-Mar-06 9:48 
QuestionHow to use? Pin
strent0021-Dec-05 5:54
strent0021-Dec-05 5:54 
GeneralCool! Pin
Super Lloyd19-Dec-05 16:36
Super Lloyd19-Dec-05 16:36 
GeneralClicking on current date in dropdown doesn't show datetime Pin
jfk8015-Mar-05 2:38
jfk8015-Mar-05 2:38 
Generalbind data to date time picker Pin
ccloon20-Feb-05 22:11
ccloon20-Feb-05 22:11 
GeneralRe: bind data to date time picker Pin
Claudio Grazioli28-Mar-05 10:36
Claudio Grazioli28-Mar-05 10:36 
GeneralRe: bind data to date time picker Pin
Claudio Grazioli24-May-05 22:45
Claudio Grazioli24-May-05 22:45 
AnswerRe: bind data to date time picker Pin
alc_aardvark14-May-08 6:07
alc_aardvark14-May-08 6:07 
GeneralRe: bind data to date time picker Pin
Roonda6-Apr-09 19:13
Roonda6-Apr-09 19:13 
GeneralDisplay blank space when NULL Pin
SupaHoopsa9-Feb-05 4:56
SupaHoopsa9-Feb-05 4:56 
GeneralRe: Display blank space when NULL Pin
Claudio Grazioli19-Apr-05 22:16
Claudio Grazioli19-Apr-05 22:16 
GeneralThank you Pin
jhenstock12-Oct-04 4:37
jhenstock12-Oct-04 4:37 
General&quot;Error creating window handle error&quot; on TabControl control Pin
Mark Horlbeck28-Sep-04 8:11
Mark Horlbeck28-Sep-04 8:11 

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.