CmonLib: DataSense

This somehow is a follow-up of a CodeRage DE session of mine from 2019 (in German) showing an alternative approach to connect controls to databases. Meanwhile the technology described in that session has become a part of CmonLib.

There is a whole range of DB controls in Delphi, such as TDBEdit, TDBListBoxTDBCheckBox and a few others, which usually come as special versions of the standard controls of the same name and are characterized by the additional properties DataSource and DataField, which are used to establish the connection to the field of a data source. This connection is then usually bi-directional – the data source represents the field content in the control, but edits within the control are also passed on to the data source. This feature alone has made Delphi one of the most efficient development environments for database applications from the very beginning. Even today, a simple application for editing a database table can be realized in Delphi within a few minutes.

Unfortunately there is a growing number of controls that don’t have a DB counterpart. Even more: DB controls are a VCL-only thing. FMX lacks them completely. (Yeah, I know – Live Bindings, but…)

To the rescue CmonLib recently got a new feature: DataSense

Just drop a TDataSense component from the Data Controls category onto your form or frame containing the controls you want to make data sensitive. That magically adds the properties DataSource and DataField to all controls supported by DataSense. Currently DataSense supports VCL controls derived from TCustomLabel, TCustomEdit and TDateTimePicker, while for FMX there is support for controls derived from TCustomEdit and TCustomMemo.

Although the VCL controls TCustomMaskEdit and TCustomLabeledEdit are already supported with TCustomEdit, they extend this a bit:

    • TCustomMaskEdit follows the Alignment, EditMask and Size of the linked field. It also does a ValidateEdit before saving.
    • In addition, TCustomLabeledEdit uses the fields DisplayLabel property for its EditLabel.

If there is no DataSource linked to a control or no DataField is set, the control behaves the same as if these properties were absent. So, while you can link any TLabel or TEdit to a dataset field, you are not forced to do so.

One can also use dynamically created controls, albeit obviously not by setting a DataSource and DataField property (I was tempted to implement that with class helpers, but refused to do so until multiple class helpers are properly supported). Instead TDataSense provides the methods AddDataSenseItem and FindDataSenseItem to get hands on the linked properties.

Make sure to add units Cmon.DataSense and Cmon.DataSense.VCL/FMX  to your uses clause. This is automatically done when dropping a TDataSense control onto a VCL/FMX form or frame.

Author: Uwe Raabe

Addicted to Pascal/Delphi since the late 70's

3 thoughts on “CmonLib: DataSense”

  1. Hi Uwe,
    Nice piece of code ! I like the dynamic properties addition : It is cool 🙂
    I didn’t know Cmonlib, it looks very nice.

    My 2 cents : Just install and test TDataSense, seems ok under vcl. Under FMX, I have to add just 2 assigning tests, because of VA on app exit (Basic D12 FMX app) : TCustomXXXDataSenseLink.DoLoadData – testing if Assigned(Field) then control…

Comments are closed.