Click here to Skip to main content
Licence 
First Posted 11 Nov 2003
Views 228,439
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
GeneralRe: "Error creating window handle error" on TabControl control PinmemberBrett Zimmerman16:00 5 Oct '04  
GeneralRe: "Error creating window handle error" on TabControl control PinmemberClaudio Grazioli21:02 18 Apr '05  
GeneralThanks PinmemberTom Fischer4:30 18 Jun '04  
GeneralCould you have vb.net version PinmemberTony Dong11:10 25 May '04  
GeneralNice control, but ... PinmemberSachaMartineau3:38 25 Feb '04  
GeneralRe: Nice control, but ... PinmemberChristian Graus12:40 25 May '04  
GeneralRe: Nice control, but ... PinmemberSteveHolland2:48 2 Feb '05  
Here is a VB.Net version.   In addition to allowing the date to be deleted (via F2 or Delete keys), the control's background color changes when it receives focus.
 
Public Class DateTimeControl
      Inherits System.Windows.Forms.DateTimePicker
      Private mdtmValue As Date = Now()
      Private mstrCustomFormat As String
      Private mdtpFormat As DateTimePickerFormat = DateTimePickerFormat.Long
      Private mblnSettoNull As Boolean
      Private mdtmMinValue As Date = DateTime.MinValue
      Private mstrText As String
      Private mBackBrush As SolidBrush
 
      <Description("Accepts legitimate date value or DateTime.MinValue."), MergableProperty(False), Category("Behavior")> _
      Public Shadows Property Value() As Date
            'This property definition overrides the Value property for the DateTimePicker control
            Get
                  'Return value stored in hidden variable
                  Return mdtmValue
            End Get
            Set(ByVal Value As Date)
                  'Set the hidden variable mdtValue
                  If mblnSettoNull = True Or Value = DateTime.MinValue Then
                        mblnSettoNull = False
                        mdtmValue = DateTime.MinValue
                  Else
                        mdtmValue = Value
                        MyBase.Value = mdtmValue
                  End If
            End Set
      End Property
      <Description("Accepts a legitimate date or time format string."), MergableProperty(False), Category("Behavior")> _
      Public Shadows Property CustomFormat() As String
            Get
                  Return mstrCustomFormat
            End Get
            Set(ByVal Value As String)
                  mstrCustomFormat = Value
                  MyBase.CustomFormat = mstrCustomFormat
            End Set
      End Property
      <DefaultValue(1), Description("Accepts a legitimate format such as Long, Short, Time or Custom."), MergableProperty(False), Category("Behavior")> _
Public Shadows Property Format() As DateTimePickerFormat
            Get
                  Return mdtpFormat
            End Get
            Set(ByVal Value As DateTimePickerFormat)
                  mdtpFormat = Value
                  MyBase.Format = mdtpFormat
            End Set
      End Property
 
Public Shadows Property BackColor() As Color
            Get
                  Return MyBase.BackColor
            End Get
            Set(ByVal Value As Color)
                  If Not mBackBrush Is Nothing Then
                        mBackBrush.Dispose()
                  End If
                  MyBase.BackColor = Value
                  mBackBrush = New SolidBrush(Me.BackColor)
                  Me.Invalidate()
            End Set
      End Property
      Protected Overrides Sub WndProc(ByRef m As Message)
            Const WM_ERASEBKGND As Int32 = &H14
            If m.Msg = WM_ERASEBKGND Then
                  Dim g As Graphics = Graphics.FromHdc(m.WParam)
                  If mBackBrush Is Nothing Then
                        mBackBrush = New SolidBrush(Me.BackColor)
                  End If
                  g.FillRectangle(mBackBrush, Me.ClientRectangle)
                  g.Dispose()
            Else
                  MyBase.WndProc(m)
            End If
      End Sub
      Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
            'Clear out when Delete of F2 is pressed
            If e.KeyCode = Keys.Delete OrElse e.KeyCode = Keys.F2 Then
                  mblnSettoNull = True 'Flag to indicate null value desired
                  Me.Format = DateTimePickerFormat.Custom
                  Me.CustomFormat = " "
                  Me.Value = DateTime.MinValue
            ElseIf e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Up Then
                  If IsNothing(Me.Tag) Then
                        Me.Format = DateTimePickerFormat.Custom
                        Me.CustomFormat = "MM/dd/yyyy"
 
                  Else
                        Me.Format = DateTimePickerFormat.Custom
                        Me.CustomFormat = "MM/dd/yyyy hh:mm tt"
                  End If
 
            End If
      End Sub
 
      Protected Overrides Sub OnCloseUp(ByVal eventargs As System.EventArgs)
            'This event fires when the user dismisses the graphical calendar
            Me.Format = DateTimePickerFormat.Custom
            If IsNothing(Me.Tag) Then
                  Me.CustomFormat = "MM/dd/yyyy"
            Else
                  Me.CustomFormat = "MM/dd/yyyy hh:mm tt"
            End If
            Me.Value = MyBase.Value
      End Sub
 
      Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
            Me.BackColor = System.Drawing.SystemColors.Window
      End Sub
 
      Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
            Me.BackColor = System.Drawing.Color.LightSteelBlue
      End Sub
End Class

QuestionOverrides value?? PinmemberEntLover3:32 12 Jan '04  
AnswerRe: Overrides value?? PinsussUDALO6:39 17 Feb '04  
GeneralAdd ShowUpDown property support Pinsussetranger17:11 2 Jan '04  
GeneralFiring OnValueChanged PinmemberSébastien Lorion15:38 26 Dec '03  
GeneralRe: Firing OnValueChanged PinmemberBalaji Vishnumolakala4:07 4 Nov '04  
GeneralSelecting the control with Tab PinmemberMortens_HUN0:47 7 Mar '07  
GeneralHelp! Pinmemberkvc3:47 13 Dec '03  
Generalimplementation Pinmemberkvc8:10 12 Dec '03  
GeneralRe: implementation PinmemberPham Minh Tri19:34 16 Dec '03  

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
Web03 | 2.5.120528.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