Click here to Skip to main content
Licence 
First Posted 11 Nov 2003
Views 228,715
Bookmarked 46 times

Nullable DateTimePicker

By | 17 Nov 2003 | Article
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

About the Author

Pham Minh Tri



Vietnam Vietnam

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionJust what I needed Pinmembergemini_dk4:43 20 Dec '11  
AnswerRe: Just what I needed Pinmemberjf644:57 25 Jan '12  
GeneralNo hour in sql database Pinmembercyrilcp0:16 9 Nov '10  
GeneralValidation wrong value fix PinmemberPREMSONBABY4:38 20 May '10  
GeneralRe: Validation wrong value fix Pinmemberoliwan12:16 30 Sep '10  
GeneralRe: Validation wrong value fix PinmemberDeuceDude15:46 28 Oct '11  
Generalthank you for your help! Pinmemberwuhai080914:52 5 May '10  
GeneralSet Nullable DateTimePicker default value [00/00/0000] Pinmembertinhleduc0:27 26 Feb '10  
GeneralUpdate PinmemberRomain TAILLANDIER22:37 21 Sep '09  
GeneralThis is a very insightful approach to the problem PinmemberPhil Raevsky16:01 28 Aug '09  
GeneralThis one dispaly pink if null date (VB.NET) PinmemberLarry Rouse11:32 28 Aug '09  
GeneralThis one works! (VB 2008) [modified] PinmemberJeromeyers12:55 10 Feb '09  
This is an edit because I realized, like the previous poster, than you can simply shadow the Value property of the base DateTimePicker to allow Nullable Dates. That means that it works exactly like you would want/expect it to, in databinding situations and everything.
 
Public Class NullableDateTimeControl
    Inherits System.Windows.Forms.DateTimePicker
 
    Private oldFormat As DateTimePickerFormat = DateTimePickerFormat.Short
    Private oldCustomFormat As String = Nothing
    Private _IsNull As Boolean = False
 

    Public Sub New()
        MyBase.New()
    End Sub
 

    ''' <summary>
    ''' The Date Value of the control (is Nullable, can be set to Nothing).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shadows Property Value() As Date?
        Get
 
            If Me._IsNull Then
 
                Return Nothing
 
            Else
 
                Return MyBase.Value
 
            End If
 
        End Get
        Set(ByVal newValue As Date?)
 
            If Not newValue.HasValue Then
 
                If Not Me._IsNull Then
 
                    Me.oldFormat = Me.Format
                    Me.oldCustomFormat = Me.CustomFormat
                    Me._IsNull = True
 
                End If
 
                Me.Format = DateTimePickerFormat.Custom
                Me.CustomFormat = " "
 
            Else
 
                If Me._IsNull Then
 
                    Me.Format = Me.oldFormat
                    Me.CustomFormat = Me.oldCustomFormat
                    Me._IsNull = False
 
                End If
 
                MyBase.Value = newValue.Value
 
            End If
 
        End Set
    End Property
 

    ''' <summary>
    ''' Allows the user to select a new date if the control is already null.
    ''' </summary>
    ''' <param name="eventargs"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnCloseUp(ByVal eventargs As System.EventArgs)
 
        If Control.MouseButtons = Windows.Forms.MouseButtons.None Then
 
            If Me._IsNull Then
 
                Me.Format = Me.oldFormat
                Me.CustomFormat = Me.oldCustomFormat
                Me._IsNull = False
 
            End If
 
        End If
 
        MyBase.OnCloseUp(eventargs)
 
    End Sub
 

    ''' <summary>
    ''' Overrides the base class implementation to allow the user to create a Null value by pressing the Delete key.
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyUp(e)
 
        If e.KeyCode = Keys.Delete Then
 
            Me.Value = DateTime.MinValue
 
        End If
 
    End Sub
 

End Class

 
modified on Wednesday, February 11, 2009 10:51 AM

GeneralRe: This one works! (VB 2008) PinmemberEMYNA12:40 28 Oct '09  
GeneralUse the Nullable Datetime for a more updated solution. Pinmemberchstngs5:39 7 Oct '08  
GeneralIt's really great! Thank you very much!! Pinmemberwxpwu2:48 15 Jun '08  
GeneralSmall correction PinmemberMaxxx3331:32 16 Nov '06  
GeneralRe: Small correction - an enhancement to that correction Pinmemberccbt19:27 9 Sep '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing =) [modified] PinmemberLeonunicorn1:45 4 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 [modified] Pinmemberkipotlov6:14 11 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 PinmemberLeonunicorn4:57 15 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 PinmemberLeonunicorn3:42 4 Jan '10  
QuestionNull Button? PinmemberKysonel9:48 28 Mar '06  
QuestionHow to use? Pinmemberstrent005:54 21 Dec '05  
GeneralCool! PinmemberSuper Lloyd16:36 19 Dec '05  
GeneralClicking on current date in dropdown doesn't show datetime Pinmemberjfk80@wp.pl2:38 15 Mar '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 18 Nov 2003
Article Copyright 2003 by Pham Minh Tri
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid