Click here to Skip to main content
Licence CPOL
First Posted 29 Mar 2006
Views 74,110
Downloads 1,005
Bookmarked 32 times

A date control better than DateTimePicker

By | 29 Mar 2006 | Article
Makes the input of date values as fast as the user can via keyboard. Also has the ability to pick date from a picker like in the traditional DateTimePicker.

Sample Image - Better_Date_Time_Picker.gif

Background

In a database application, date handling is a bit difficult as compared to validating other data types, and getting input by keyboard requires some extra effort. The common solution to this problem is the DateTimePicker control provided by Microsoft. Unfortunately, DateTimePicker is not the best suited for fast data entry via keyboard that is always required in database driven applications. I get several complaints from end users that they don’t want to use the mouse for date selection, or that when entering date in a DateTimePicker via keyboard, it requires extra keys to be pressed from moving the cursor from one day part to next, and so on. This is the motivation behind the development of a new DateTimePicker, which will enable users to enter dates faster and in alternate ways either by using the keyboard or by using the mouse.

Introduction

What I have done is, created a textbox masked control to input date via the keyboard, and also attached a DateTimePicker control to input date via mouse. It also has the functionality to validate user input if the date is not valid, and then the text color is displayed in red. Also, set its property “IsValid” to false. To validate date, what I have done is a trick: just set the value of the DateTimePicker with the value entered in the TextBox, and the DateTimePicker will generate an exception for an invalid date value and the color will change to red.

Using the Control

It is quite easy to use like other controls. Just add to your toolbox, and drop it on the form you want to use. I will give here an overview of the properties that can be used during design time and at run time.

I have tried to expose properties just like in the DateTimePicker, so if you are going to replace DateTimePicker from your existing project with this one, you don’t have to change your code.

Properties

  • Value
  • Returns the value in the form of date; same as in DateTimePicker.

  • Day
  • Same as DateTimePicker’s ‘Value.Day’, but you will get one step in advance, simply ‘Control1.Day’.

  • Month
  • Same as in DateTimePicker’s ‘Value.Month’, but you will get one step in advance, simply ‘Control1.Month’.

  • Year
  • Same as in DateTimePicker’s ‘Value.Year’, but you will get one step in advance, simply ‘Control1.Year’.

  • SQLValue
  • The specific property required when using the value of DateTimePicker with SQL Server, because SQL Server requires a date format of “yyyy\MM\dd” to work fine. This property returns the formatted date.

  • DateFormat
  • Specify the date format you want to use; currently, there are three supported formats: DMY, MDY, YDM.

  • IsValid
  • A read-only property that returns the state of the control. This property can be used for validating the form input before submitting back to the DBMS.

  • AllowNull
  • This property is used to replace the checkbox that is displayed with the default DateTimePicker, that often creates a problem whenever you try to check or uncheck it in code. So for now, when we need to have a null-able date column, this property will be used either to allow a null or not. It will return null if the textbox is empty, which means the user doesn’t want to enter the date.

Events

  • ValueChanged
  • You can write your custom code for the value changed event as we write for DateTimePicker.

Methods

  • Clear
  • It can be used to clear the date if AllowNull is set to true; then it will set the value property to null and clear the text of the control; otherwise, sets the control text to the current date and value accordingly.

License

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

About the Author

Muhammad Abubakar Dar

Software Developer (Senior)
Systems Limited
Pakistan Pakistan

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
QuestionDate Time Picker PinmemberKen Beale2:24 1 Apr '12  
QuestionDatabinding and Parse event Pinmembere_ch9:35 16 Sep '11  
GeneralRecommendations to a great control Pinmemberkevinlkingma5:03 8 Dec '08  
General.NET 2.0 PinmemberSuneelan1:56 5 Oct '07  
GeneralC# Source File PinmemberMary_m7:33 14 Sep '07  
GeneralDataBinding Pinmembercagla129:30 11 May '07  
QuestionPassword for Source? PinmemberDaryl_Saunders17:14 26 Jan '07  
GeneralNot quite there Pinmemberl2l222:53 16 Nov '06  
QuestionData Binding Pinmemberhwdevelop7:42 16 Nov '06  
GeneralGreat control PinmemberPinx11:03 13 Nov '06  
GeneralExcellent for superfast date entry! PinmemberCygNuZ22:50 9 Oct '06  

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
Web02 | 2.5.120517.1 | Last Updated 30 Mar 2006
Article Copyright 2006 by Muhammad Abubakar Dar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid