The TDateTimePicker component of the Visual Component Library is a visual component designed specifically for inputting dates or times. In the dmComboBox date mode, it resembles a list box or combo box, except that the drop-down list is replaced with a calendar illustration where users can select a date from the calendar. Dates or times can also be selected by scrolling with Up and Down arrows and by typing. It is derived from the TCommonCalendar class, a base class used when creating custom controls that represent dates in a calendar-like format. It is declared in the ComCtrl.pas unit.
This article shows how a data-aware DateTimePicker can be used and how it was developed. First of all, it was derived from the TDateTimePicker class of the VCL as follows:
TGtroDBDateTimePicker = class(TDateTimePicker) private FCloseAction: TCloseActions; FDataLink: TFieldDataLink; // we link only one field ... protected procedure Loaded; override; public constructor Create(Owner: TComponent); override; destructor Destroy; override; published property CloseAction: TCloseActions read FCloseAction write FCloseAction; property DataField: string read GetDataField write SetDataField; property DataSource: TDataSource read GetDataSource write SetDataSource; property ReadOnly: boolean read GetReadOnly write SetReadOnly Default true; end;
including a datalink private member variable and publishing three new properties: CloseAction, DataField and DataSource.
This new component adds only one capability to the TDateTimePicker component: its data-awareness. The Date or Time property will be linked to a date field or a time field in a data set thus modifying this field in a non-permanent way (the user must do an explicit post to make it permanent) through its OnClose or its OnChange event).
Generally, database programs connect to some data-aware control through a DataSource component, and then connect the DataSource component to a data set, usually a TTable or a TQuery. The connection between a data-aware control and the TDataSource is called a data link, and is represented programmatically by an object of class TDataLink declared and implemented in DB.pas. Though TDataLink is not technically an abstract class, it is seldom used directly. Either you use one of the Delphi-provided data link classes derived from it or you derive a new one yourself. Here, we declared the FDatalink private member variable as a TFieldDataLink, using this Delphi-provided datalink, since we link to only one field of the dataset. I won't expand of the theory as it has been developed in two separate articles entitled respectively "A data-aware Pushbutton Calendar component" and "A data-aware Pushbutton Calendar component".
There was no need to elaborate much about the datalink as we could use a Delphi-provided datalink but the field of the dataset that we must link to contains a date. A custom property editor was therefore required for the DataField property in order to avoid selecting fields that cannot contain date formats. We derived the TGtroDBPushButtonCalendarDataFieldEditor field editor from the TStringProperty class: this process is detailed in a separate article entitled "A data-aware Pushbutton Calendar component".
How to use
The source code of this component can be downloaded here. This code contains three data-aware components contained in a package called gtrodblib6.dpk. Once the gtrodblib6 package is compiled and installed on Delphi 6, the component becomes available in the gtro pane of the component palette. From there, is can be dropped on a form or on a frame. The component is then displayed on the form or the frame and you give it the name that you want. The object inspector displays the published its properties and event handlers. The most important are:
- DataSource which must be connected to a dataset;
- FieldName which must be connected to a date field of the dataset (ftDate or ftTime);
- CloseAction is set to either dtnChange (changes the dataset when the date changes) or dtnClose (changes the dataset only when the component is closed);
Once these have been initialized, the component is ready for use on the form or on the frame.
This component is a direct data-aware supplement to the TDateTimePicker component of the VCL in about the same way that the TDBEdit, TDBMemo, TDBListBox or TDBComboBox components of the DataControls pane of the VCL are supplements to their counterparts of the Standard pane. The only thing that this component does in addition to the capabilities of the TDateTimePicker component is to modify the content of a dataset.
In order to link the DateTimePicker to a dataset, we had to develop a datalink and publish the DataSource and the FieldName properties. In order to restrict the selection of the FieldName property to a date field, we have developed a field editor. The rest of the article has described how the component can be used and how it works.