1. Home
  2. Computing & Technology
  3. Delphi Programming
TadpDBDateTimePicker - data aware TDateTimePicker Delphi component
Full source code of the TadpDBDateTimePicker Delphi component, a data-aware version of the standard TDateTimePicker - a visual component designed specifically for manipulating date and/or time (TDateTime) values. The TadpDBDateTimePicker knows how to handle "null" database values, and can show week numbers in the calendar.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download FULL source
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• A Beginner's Guide to Database programming
• Building Delphi components - tutorials
• Free source Delphi components
• DateTimePicker inside a DBGrid

As you know, Delphi provides many components that resemble standard controls but are data-aware. Data-aware controls (located on the Data Controls page of the Component palette) such as TDBEdit (DB version of the TEdit component) and TDBCheckBox (data-aware TCheckBox) automatically display the values associated with field components, beside being able to display data, data-aware controls can also send new and changed values to the database.

If you take a look at the Data Controls page you'll find more than 15 data-aware controls - all designed to automate the process of data editing and posting changes back to the database. If you take another look, you'll find something is missing: there's no control that can handle TDateTimeFields.

TadpDBDateTimePicker
121503_1 (1K) TadpDBDateTimePicker is a data-aware descendant of the native TDateTimePicker control. Being a data-aware means that there are two new properties: DataSource - use it to connect to a TDataSource component; and a DataField - use it to connect the component to a date-time field. The TadpDBDateTimePicker control displays blank for a null field value; includes a ShowWeekNumbers property - when set to True week numbers are displayed in the drop-down calendar.

TadpDBDateTimePicker displaying NULL value TadpDBDateTimePicker calendar with week numbers

Warning ;)
The TadpDateTimePicker control is by no means perfect. It can be used to handle database fields that map to TDateField components, but it doesn't behave well with TTimeField fields (Kind property set to dtkTime).

Here's a thought from Peter Below (one of TeamB members): "The truth is that MS's DateTimePicker is badly designed fairly inflexible as a consequence. There is no method to get it to drop down or close up under program control other than faking keyboard input. There is no easy way to intercept the dropdown in a way that allows one to cancel it. To implement this one would indeed have to make a derived class and trap the users mouse and keyboard input to handle input events that would drop down the calendar. It may be easier to build your own picker from a TMaskEdit control, a TButton and a TMonthCalendar..."

Installing into a Component palette
First, download the component. The TadpDBDateTimePicker comes as a single unit file (.pas extension). You'll need to add the component into an existing package. Here's "How to Install Custom Component in Delphi (into Existing Package)"

Questions? Comments? Extensions? Exceptions?!
That's it. If you find this component practical and if you extend it by adding more properties, please send your source and make it available to other developers.
As always if there are any questions or comments please post them on the Delphi Programming Forum.

~ Zarko Gajic

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.