Click here to Skip to main content
Licence 
First Posted 11 Nov 2003
Views 220,430
Downloads 2,883
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  
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  
When NullableDateTimePicker (NDTP) state is null and it doesn't show value, click on the dropdown and select current value at the bottom of the dropdown, near red circle mark. This should change the NDTP state to not-null and show current DateTime. But NDTP checks if user selected value in dropdown by checking that no mouse button is pressed (in OnCloseUp), which is true only if a calendar was clicked, but not if bottom mark was clicked.
IMO, there is solution for this is not to rely on mouse button state in OnCloseUp. Instead override OnValueChanged and set a flag, then use it in OnCloseUp.
Thus:
 

protected override void OnCloseUp(EventArgs eventargs) {
if (Control.MouseButtons == MouseButtons.None || fHasValueChanged) {
if (fIsNull) {
this.Format = fOldFormat;
this.CustomFormat = fOldCustomFormat;
fIsNull = false;
}
fHasValueChanged = false;
}
base.OnCloseUp(eventargs);
}
protected override void OnKeyUp(KeyEventArgs e) {
base.OnKeyUp(e);
if (e.KeyCode == Keys.Delete) {
this.Value = DateTime.MinValue;
this.fHasValueChanged = false;
}
}
protected override void OnValueChanged(EventArgs eventargs) {
fHasValueChanged = true;
base.OnValueChanged(eventargs);
}

 
Resetting to false in OnKeyUp has to be done, because user can change value by hand, not by dropdown. In such case the flag remains true which could cause problems if user presses delete on NDTP and then pressed and depressed dropdown arrow (without changing value). If flag is not reset when she pressed delete, than suddenly NDTP would show value.

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