Click here to Skip to main content
Licence 
First Posted 11 Nov 2003
Views 220,379
Downloads 2,875
Bookmarked 46 times

Nullable DateTimePicker

By Pham Minh Tri | 17 Nov 2003
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.
1 vote, 3.1%
1
1 vote, 3.1%
2
1 vote, 3.1%
3
6 votes, 18.8%
4
23 votes, 71.9%
5
4.74/5 - 32 votes
2 removed
μ 4.57, σa 1.68 [?]

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_dk5:43 20 Dec '11  
AnswerRe: Just what I needed Pinmemberjf645:57 25 Jan '12  
GeneralNo hour in sql database Pinmembercyrilcp1:16 9 Nov '10  
GeneralValidation wrong value fix PinmemberPREMSONBABY5:38 20 May '10  
GeneralRe: Validation wrong value fix Pinmemberoliwan13:16 30 Sep '10  
GeneralRe: Validation wrong value fix PinmemberDeuceDude16:46 28 Oct '11  
Generalthank you for your help! Pinmemberwuhai080915:52 5 May '10  
GeneralSet Nullable DateTimePicker default value [00/00/0000] Pinmembertinhleduc1:27 26 Feb '10  
GeneralUpdate PinmemberRomain TAILLANDIER23:37 21 Sep '09  
GeneralThis is a very insightful approach to the problem PinmemberPhil Raevsky17:01 28 Aug '09  
GeneralThis one dispaly pink if null date (VB.NET) PinmemberLarry Rouse12:32 28 Aug '09  
Imports System
Imports System.Windows.Forms
Namespace Controls
Public Class DateTimePicker
Inherits System.Windows.Forms.DateTimePicker
Private bIsNull As Boolean = False
Private mBackColor As Color = SystemColors.Window
Public Sub New()
MyBase.New()
End Sub
Public Shadows Property Value() As DateTime
Get
If bIsNull Then
Return DateTime.MinValue
Else
Return MyBase.Value
End If
End Get
Set(ByVal value As DateTime)
If value = DateTime.MinValue Or value.Date = "01/01/1905" Or value.Date = "01/01/1900" Then
If bIsNull = False Then bIsNull = True
MakePinkFormat()
Else
MakeWhiteFormat()
bIsNull = False
MyBase.Value = value
End If
End Set
End Property
' Protected Overloads Overrides Sub OnCloseUp(ByVal eventargs As EventArgs)
Protected Overrides Sub OnCloseUp(ByVal eventargs As EventArgs)
 
If Control.MouseButtons = MouseButtons.None Or Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
Me.Value = MyBase.Value
End If
MyBase.OnCloseUp(eventargs)
End Sub
Protected Overloads Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
MyBase.OnKeyDown(e)
 
If e.KeyCode = Keys.Delete Then
Me.Value = DateTime.MinValue
MyBase.Value = "01/01/1905"
End If
End Sub
Private Sub MakePinkFormat()
Me.Format = DateTimePickerFormat.[Custom]
Me.BackColor = Color.Pink
Me.CustomFormat = " "
MyBase.Value = "01/01/1905"
End Sub
Private Sub MakeWhiteFormat()
Me.Format = DateTimePickerFormat.Short
Me.BackColor = Color.White
Me.CustomFormat = Nothing
End Sub
'The BackColor property we will be calling
Public Overrides Property BackColor() As Color
Get
Return mBackColor
End Get
Set(ByVal Value As Color)
mBackColor = Value
'After the BackColor has been set, Invalidate the control
'This will force it to be redrawn
'Me.Invalidate()
End Set
End Property
'WndProc fires durring the painting of the control
Protected Overrides Sub WndProc(ByRef m As Message)
'Check to see if message being send is WM_ERASEBKGND.
'The hex value of this message is &H14.
'This message is sent when the background of the
'object needs to be erased. In our case though, instead of
'erasing it, we will paint a rectangle over it
If m.Msg = CInt(&H14) Then ' WM_ERASEBKGND
Dim g As Graphics = Graphics.FromHdc(m.WParam)
g.FillRectangle(New SolidBrush(mBackColor), ClientRectangle)
g.Dispose()
Return
End If
MyBase.WndProc(m)
End Sub
Private Sub DateTimePicker_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnabledChanged
Me.CalendarForeColor = Color.Black
End Sub
End Class
End Namespace
GeneralThis one works! (VB 2008) [modified] PinmemberJeromeyers13:55 10 Feb '09  
GeneralRe: This one works! (VB 2008) PinmemberEMYNA13:40 28 Oct '09  
GeneralUse the Nullable Datetime for a more updated solution. Pinmemberchstngs6:39 7 Oct '08  
GeneralIt's really great! Thank you very much!! Pinmemberwxpwu3:48 15 Jun '08  
GeneralSmall correction PinmemberMaxxx3332:32 16 Nov '06  
GeneralRe: Small correction - an enhancement to that correction Pinmemberccbt20:27 9 Sep '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing =) [modified] PinmemberLeonunicorn2:45 4 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 [modified] Pinmemberkipotlov7:14 11 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 PinmemberLeonunicorn5:57 15 Dec '09  
GeneralRe: Small correction - an enhancement to that correction BugFixing x 2 PinmemberLeonunicorn4:42 4 Jan '10  
QuestionNull Button? PinmemberKysonel10:48 28 Mar '06  
QuestionHow to use? Pinmemberstrent006:54 21 Dec '05  
GeneralCool! PinmemberSuper Lloyd17:36 19 Dec '05  
GeneralClicking on current date in dropdown doesn't show datetime Pinmemberjfk80@wp.pl3: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
Web01 | 2.5.120210.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