Click here to Skip to main content
Click here to Skip to main content
Go to top

Complete Date/Time Picker Control

, 8 Aug 2013
Rate this:
Please Sign up or sign in to vote.
A complete replacement to Microsoft's Date/Time Picker control.

Introduction 

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:  

  • ForeColor   
  • BackColor  
  • Font 
  • ContextMenuStrip
  • SelectionStart 
  • 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.

History

  • 07/31/2013 - Initial release.

License

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

Share

About the Author

Peter T. Ringering
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

 
QuestionInavlid date format PinmemberJaineet Malhi13-Sep-14 19:04 
AnswerRe: Inavlid date format PinprofessionalPeter T. Ringering14-Sep-14 8:01 
GeneralRe: Inavlid date format PinmemberJaineet Malhi14-Sep-14 13:55 
GeneralRe: Inavlid date format PinprofessionalPeter T. Ringering14-Sep-14 16:15 
GeneralRe: Inavlid date format PinprofessionalPeter T. Ringering14-Sep-14 17:42 
QuestionData binding PinmemberArend Jan21-Mar-14 4:50 
AnswerRe: Data binding PinprofessionalPeter T. Ringering21-Mar-14 6:19 
QuestionTHANKS PinmemberMember 1046517417-Dec-13 9:43 
AnswerRe: THANKS PinmemberPeter T. Ringering17-Dec-13 10:55 
GeneralRe: THANKS PinmemberMember 1046517418-Dec-13 7:48 
QuestionThanks a lot. Pinmemberjaylala6-Dec-13 19:30 
AnswerRe: Thanks a lot. PinmemberPeter T. Ringering7-Dec-13 6:30 
GeneralMy vote of 5 Pinmemberfredatcodeproject10-Aug-13 6:59 
QuestionOut of curiosity... PinprotectorMarc Clifton9-Aug-13 8:42 
AnswerRe: Out of curiosity... PinmemberPeter T. Ringering9-Aug-13 10:53 
GeneralMy vote of 4 Pinmemberfredatcodeproject9-Aug-13 5:36 
GeneralRe: My vote of 4 PinmemberPeter T. Ringering9-Aug-13 10:28 
GeneralRe: My vote of 4 Pinmemberfredatcodeproject10-Aug-13 7:00 
Questioncould not download zip files for source code Error PinmemberTridip Bhattacharjee8-Aug-13 21:00 
AnswerRe: could not download zip files for source code Error PinmemberPeter T. Ringering9-Aug-13 10:27 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 8 Aug 2013
Article Copyright 2013 by Peter T. Ringering
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid