Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / Visual Basic

A date control better than DateTimePicker

Rate me:
Please Sign up or sign in to vote.
3.97/5 (20 votes)
29 Mar 2006CPOL3 min read 147.4K   7K   35   15
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)


Written By
Software Developer (Senior) Systems Limited
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1454928531-Oct-19 3:31
Member 1454928531-Oct-19 3:31 
QuestionIf we set any date value in nullable datetimepicker and again clear it using delete key. Pin
ramankumarsingla13-Apr-17 21:17
ramankumarsingla13-Apr-17 21:17 
QuestionRe: If we set any date value in nullable datetimepicker and again clear it using delete key. Pin
Mark Clark19-Apr-18 7:17
Mark Clark19-Apr-18 7:17 
Answerthnkz for this control Pin
Pachacutec236-Sep-12 9:15
Pachacutec236-Sep-12 9:15 
QuestionDate Time Picker Pin
Ken Beale1-Apr-12 2:24
Ken Beale1-Apr-12 2:24 
QuestionDatabinding and Parse event Pin
e_ch16-Sep-11 9:35
e_ch16-Sep-11 9:35 
GeneralRecommendations to a great control PinPopular
kevinlkingma8-Dec-08 5:03
kevinlkingma8-Dec-08 5:03 
General.NET 2.0 Pin
Suneelan5-Oct-07 1:56
Suneelan5-Oct-07 1:56 
GeneralC# Source File Pin
Mary_m14-Sep-07 7:33
Mary_m14-Sep-07 7:33 
GeneralDataBinding Pin
cagla1211-May-07 9:30
cagla1211-May-07 9:30 
QuestionPassword for Source? Pin
Daryl_Saunders26-Jan-07 17:14
Daryl_Saunders26-Jan-07 17:14 
GeneralNot quite there Pin
l2l216-Nov-06 22:53
l2l216-Nov-06 22:53 
QuestionData Binding Pin
hwdevelop16-Nov-06 7:42
hwdevelop16-Nov-06 7:42 
GeneralGreat control Pin
pinx13-Nov-06 11:03
pinx13-Nov-06 11:03 
GeneralExcellent for superfast date entry! Pin
CygNuZ9-Oct-06 22:50
CygNuZ9-Oct-06 22:50 

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.