Click here to Skip to main content
15,920,602 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 419.1K   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

 
General"Error creating window handle error" on TabControl control Pin
Mark Horlbeck28-Sep-04 8:11
Mark Horlbeck28-Sep-04 8:11 
GeneralRe: "Error creating window handle error" on TabControl control Pin
Member 9440325-Oct-04 16:00
Member 9440325-Oct-04 16:00 
GeneralRe: "Error creating window handle error" on TabControl control Pin
Claudio Grazioli18-Apr-05 21:02
Claudio Grazioli18-Apr-05 21:02 
GeneralThanks Pin
Tom Fischer18-Jun-04 4:30
Tom Fischer18-Jun-04 4:30 
GeneralCould you have vb.net version Pin
Tony Dong25-May-04 11:10
Tony Dong25-May-04 11:10 
GeneralNice control, but ... Pin
SachaMartineau25-Feb-04 3:38
SachaMartineau25-Feb-04 3:38 
GeneralRe: Nice control, but ... Pin
Christian Graus25-May-04 12:40
protectorChristian Graus25-May-04 12:40 
GeneralRe: Nice control, but ... Pin
SteveHolland2-Feb-05 2:48
SteveHolland2-Feb-05 2:48 
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?? Pin
EntLover12-Jan-04 3:32
EntLover12-Jan-04 3:32 
AnswerRe: Overrides value?? Pin
UDALO17-Feb-04 6:39
sussUDALO17-Feb-04 6:39 
GeneralAdd ShowUpDown property support Pin
Sebastien Arcand2-Jan-04 17:11
Sebastien Arcand2-Jan-04 17:11 
GeneralFiring OnValueChanged Pin
Sebastien Lorion26-Dec-03 15:38
Sebastien Lorion26-Dec-03 15:38 
GeneralRe: Firing OnValueChanged Pin
Balaji Vishnumolakala4-Nov-04 4:07
Balaji Vishnumolakala4-Nov-04 4:07 
GeneralSelecting the control with Tab Pin
Mortens_HUN7-Mar-07 0:47
Mortens_HUN7-Mar-07 0:47 
GeneralHelp! Pin
kvc13-Dec-03 3:47
kvc13-Dec-03 3:47 
Generalimplementation Pin
kvc12-Dec-03 8:10
kvc12-Dec-03 8:10 
GeneralRe: implementation Pin
Pham Minh Tri16-Dec-03 19:34
Pham Minh Tri16-Dec-03 19:34 

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.