Click here to Skip to main content
Click here to Skip to main content

A date control better than DateTimePicker

By , 29 Mar 2006
Rate this:
Please Sign up or sign in to vote.

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
No Biography provided

Comments and Discussions

 
Answerthnkz for this control PinmemberPachacutec236-Sep-12 9:15 
QuestionDate Time Picker PinmemberKen Beale1-Apr-12 2:24 
QuestionDatabinding and Parse event Pinmembere_ch16-Sep-11 9:35 
GeneralRecommendations to a great control Pinmemberkevinlkingma8-Dec-08 5:03 
General.NET 2.0 PinmemberSuneelan5-Oct-07 1:56 
GeneralC# Source File PinmemberMary_m14-Sep-07 7:33 
GeneralDataBinding Pinmembercagla1211-May-07 9:30 
QuestionPassword for Source? PinmemberDaryl_Saunders26-Jan-07 17:14 
GeneralNot quite there Pinmemberl2l216-Nov-06 22:53 
QuestionData Binding Pinmemberhwdevelop16-Nov-06 7:42 
GeneralGreat control PinmemberPinx13-Nov-06 11:03 
GeneralExcellent for superfast date entry! PinmemberCygNuZ9-Oct-06 22:50 

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