Click here to Skip to main content
15,945,119 members
Articles / Programming Languages / C#

Complete Date/Time Picker Control

Rate me:
Please Sign up or sign in to vote.
4.71/5 (9 votes)
8 Aug 2013CPOL4 min read 80.9K   4.9K   14   26
A complete replacement to Microsoft's Date/Time Picker control.

Image 1

Image 2


I'm currently working on a home finance application and needed a date control.  The one provided by Microsoft is OK but very limited.  Its problems are: 

  • No easy way for a user to enter a null date.
  • No way to select the whole date to either re-type in the date or null it out.  This was especially a problem when I embedded Microsoft's date control inside a DataGridView control with the edit mode set to EditOnEnter.  I wanted it set so if everything was selected on the control, then the arrow keys would move to the next or previous cell.  Otherwise the cursor would move in that cell.
  • While editing the date, the cursor doesn't move automatically to the next date segment.  The user has to type in the date separator character or the arrow keys to move to the next date segment. This makes data entry cumbersome and slow.   

I did some looking around in CodeProject and other web sites and couldn't find what I needed.  So, I decided to write this date control.  Its features are: 

  • Displays a date and/or time in any valid Microsoft date format.
  • When entering dates, the cursor automatically skips over date separator characters like '-' and '/' which makes for smooth and fast data entry for the user. 
  • The control validates on entry to make sure the user doesn't enter invalid date sections (ex. entering a month value over 12).
  • The drop down button, when clicked, shows the standard calendar control and supports Min/Max Date properties. 
  • When the user enters a date/time value, an entry pattern is shown (ex. 00/00/0000) so the user knows which date segment he/she is currently editing. 
  • Nulling-out the control is easy.  Simply select everything in the control and hit Back Space or Delete and everything reverts back to the date/time entry pattern.  Tab out from there and the control will show blank.     

There are a couple limitations:   

  • When entering a date and time, the user has to enter all digits for the date/time segment.  For example, if you set the DateFormat property to be "M/d/yy",  when the user tries to enter a date, he/she will need to enter "03/03/2013" (excluding the slashes) instead of "3/3/13 ". This is necessary so the user doesn't have to enter the slashes and so that entry validation knows where the user is and can validate accordingly.  However, once you leave the control, it will show (per the example) "3/3/13".  Even though the user has to enter a few more digits, it's faster than entering slashes or dashes. 
  • Any valid DateFormat & TimeFormat property values are allowed as long as today's date can be formatted with the Date/Time Format property values and then immediately converted back to a DateTime variable. If the validation fails anywhere in the 2 above procedures, then a messagebox will show.  

Using the code 

Using the code is quite simple.  Just drop in the CompleteDateTimePicker.dll into the toolbox or add the project to your solution.  Then drag a CompleteDateTimePickerControl onto your form and that's it.

There are 2 controls in this control:  A TextBox and a Button. 

The control will start with a MM/dd/yyyy DateFormat property value.  The TimeFormat will be blank.  You'll notice that the entry pattern will show "00/00/0000".  This entry pattern will only show in design mode or if the control gets focus.  This is to aid in sizing the control.  If you change the DateFormat or TimeFormat property values, the entry pattern will also update.  

Use the Text property to get and set the text in the TextBox part of the control.  If the text in the TextBox control is just the date/time entry pattern, then the Text property will return blank.  In setting the property, the class will format the value and set the text.  If the value cannot be converted to a date, then the computer will beep. 

Use the Value property to convert the current text into a DateTime variable and return.  If the text in the control is not a valid date/time, then the computer will beep.  To avoid this, check the ValidDate property value first which returns IsDateValidResult.Valid if the text in the control is a valid date.  Setting the property is the best way to set the control's value.  

The FormattedText property returns whatever is in the TextBox control as is.

The following properties affect the TextBox control:  

  • <code>ForeColor   
  • <code>BackColor  
  • <code>Font 
  • <code>ContextMenuStrip
  • <code>SelectionStart 
  • <code>SelectionLength   

There's 1 event:  ValueChanged (virtual OnValueChanged) which is fired when the date in the TextBox control changes.

Points of Interest

Overall, I have learned a lot while making this control and I look forward to people's comments and critiques about it.


  • 07/31/2013 - Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Ring2 Software
United States United States
I've been a computer nerd all of my life.

Graduated 1995 from Walla Walla University with a B.S. degree in Business Education.

Since then, I've worked at various IT jobs.

In 1999, I started work as a Technical Support Specialist at Cougar Mountain Software. I then worked my way up over the years to Senior Programmer/Analyst.

On September 2005, I was in a car accident which left me paralyzed from the chest down and my fingers were also paralyzed.

Since then, I've been living on disability and working on a couple software projects here at home.

Comments and Discussions

Question5 Star Pin
hossein913-Mar-17 12:32
professionalhossein913-Mar-17 12:32 
Questionx86 configuration Pin
Member 1046517415-Jul-16 6:23
Member 1046517415-Jul-16 6:23 
AnswerRe: x86 configuration Pin
Peter T. Ringering15-Jul-16 7:21
professionalPeter T. Ringering15-Jul-16 7:21 
QuestionWell done and a suggestion Pin
SBendBuckeye19-Jul-15 17:07
SBendBuckeye19-Jul-15 17:07 
AnswerRe: Well done and a suggestion Pin
Peter T. Ringering21-Jul-15 14:18
professionalPeter T. Ringering21-Jul-15 14:18 
QuestionInavlid date format Pin
Jaineet Malhi13-Sep-14 19:04
Jaineet Malhi13-Sep-14 19:04 
AnswerRe: Inavlid date format Pin
Peter T. Ringering14-Sep-14 8:01
professionalPeter T. Ringering14-Sep-14 8:01 
GeneralRe: Inavlid date format Pin
Jaineet Malhi14-Sep-14 13:55
Jaineet Malhi14-Sep-14 13:55 
GeneralRe: Inavlid date format Pin
Peter T. Ringering14-Sep-14 16:15
professionalPeter T. Ringering14-Sep-14 16:15 
GeneralRe: Inavlid date format Pin
Peter T. Ringering14-Sep-14 17:42
professionalPeter T. Ringering14-Sep-14 17:42 
QuestionData binding Pin
Arend Jan21-Mar-14 4:50
Arend Jan21-Mar-14 4:50 
AnswerRe: Data binding Pin
Peter T. Ringering21-Mar-14 6:19
professionalPeter T. Ringering21-Mar-14 6:19 
AnswerRe: Data binding Pin
Member 1046517414-Nov-14 5:59
Member 1046517414-Nov-14 5:59 
QuestionTHANKS Pin
Member 1046517417-Dec-13 9:43
Member 1046517417-Dec-13 9:43 
AnswerRe: THANKS Pin
Peter T. Ringering17-Dec-13 10:55
professionalPeter T. Ringering17-Dec-13 10:55 
GeneralRe: THANKS Pin
Member 1046517418-Dec-13 7:48
Member 1046517418-Dec-13 7:48 
QuestionThanks a lot. Pin
jaylala6-Dec-13 19:30
jaylala6-Dec-13 19:30 
AnswerRe: Thanks a lot. Pin
Peter T. Ringering7-Dec-13 6:30
professionalPeter T. Ringering7-Dec-13 6:30 
GeneralMy vote of 5 Pin
fredatcodeproject10-Aug-13 6:59
professionalfredatcodeproject10-Aug-13 6:59 
QuestionOut of curiosity... Pin
Marc Clifton9-Aug-13 8:42
mvaMarc Clifton9-Aug-13 8:42 
AnswerRe: Out of curiosity... Pin
Peter T. Ringering9-Aug-13 10:53
professionalPeter T. Ringering9-Aug-13 10:53 
GeneralMy vote of 4 Pin
fredatcodeproject9-Aug-13 5:36
professionalfredatcodeproject9-Aug-13 5:36 
GeneralRe: My vote of 4 Pin
Peter T. Ringering9-Aug-13 10:28
professionalPeter T. Ringering9-Aug-13 10:28 
GeneralRe: My vote of 4 Pin
fredatcodeproject10-Aug-13 7:00
professionalfredatcodeproject10-Aug-13 7:00 
Questioncould not download zip files for source code Error Pin
Tridip Bhattacharjee8-Aug-13 21:00
professionalTridip Bhattacharjee8-Aug-13 21:00 

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.