Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to create data-aware components Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas 

How to create data-aware components


This document describes minimal steps necessary to create a data-aware browsing 
component that displays data for a single field. The example component is a panel 
with DataSource and DataField properties similar to the TDBText component. See the 
Component Writer's Guide "Making a Control Data-Aware" for further examples.

Basic steps to create a data-browsing component

Create or derive a component that allows the display, but not the entry of data. 
For instance, you could use a TMemo with ReadOnly set to true. In the example 
outlined in this document, we'll use a TCustomPanel. The TCustomPanel will allow 
display, but not data entry.

Add a data-link object to your component. This object manages communication between 
the component and the database table.
Add DataField and DataSource properties to the component.
Add methods to get and set the DataField and DataSource.
Add a DataChange method the component to handle the data-link object's OnDataChange 
Override the component constructor to create the datalink and hook up the 
DataChange method.
Override the component destructor to cleanup the datalink.

Creating the TDBPanel

Create or derive a component that allows the display, but not the entry of data. 
We'll be using a TCustomPanel as a starting point for this example.

Choose the appropriate menu option to create a new component (this will vary 
between editions of Delphi), and specify TDBPanel as the class name, and 
TCustomPanel as the Ancestor type. You may specify any palette page.

Add DB and DBTables to your Uses clause.

Add a data-link object to the components private section. This example will display 
data for a single field, so we will use a TFieldDataLink to provide the connection 
between our new component and a DataSource. Name the new data-link object 
FDataLink. Example:

2   private
3   FDataLink: TFieldDataLink;

Add DataField and DataSource properties to the component. We will add supporting 
code for the get and set methods in following steps. Note: Our new component will 
have DataField and DataSource properties and FDataLink will also have its own 
DataField and Datasource properties. Example:

5   published

property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;

Add private methods to get and set the DataField and DataSource property values to 
and from the DataField and DataSource for FDataLink. Example:

6   private
7   FDataLink: TFieldDataLink;
9   function GetDataField: string;
10  function GetDataSource: TDataSource;
11  procedure SetDataField(const Value: string);
12  procedure SetDataSource(Value: TDataSource);
14  implementation
16  function TDBPanel.GetDataField: string;
17  begin
18    Result := FDataLink.FieldName;
19  end;
21  function TDBPanel.GetDataSource: TDataSource;
22  begin
23    Result := FDataLink.DataSource;
24  end;
26  procedure TDBPanel.SetDataField(const Value: string);
27  begin
28    FDataLink.FieldName := Value;
29  end;
31  procedure TDBPanel.SetDataSource(Value: TDataSource);
32  begin
33    FDataLink.DataSource := Value;
34  end;

Add a private DataChange method to be assigned to the datalink's OnDataChange 
event. In the DataChange method add code to display actual database field data 
provided by the data-link object. In this example, we assign FDataLink's field 
value to the panel's caption. Example:

36  private
38  procedure DataChange(Sender: TObject);
40  implementation
42  procedure TDBPanel.DataChange(Sender: TObject);
43  begin
44    if FDataLink.Field = nil then
45      Caption := '';
46  else
47    Caption := FDataLink.Field.AsString;
48  end;

Override the component constructor Create method. In the implementation of Create, 
create the FDataLink object, and assign the private DataChange method to 
FDataLink's OnDataChange event. Example:

49  public
51  constructor Create(AOwner: TComponent); override;
53  implementation
55  constructor TMyDBPanel.Create(AOwner: TComponent);
56  begin
57    inherited Create(AOwner);
58    FDataLink := TFieldDataLink.Create;
59    FDataLink.OnDataChange := DataChange;
60  end;

Override the component destructor Destroy method. In the implementation of Destroy, 
set OnDataChange to nil (avoids a GPF), and free FDatalink. Example:

61  public
63  destructor Destroy; override;
65  implementation
67  destructor TDBPanel.Destroy;
68  begin
69    FDataLink.OnDataChange := nil;
70    FDataLink.Free;
71    inherited Destroy;
72  end;

Save the unit and install the component (see the Users Guide, and the Component 
Writers Guide for more on saving units and installing components).

To test the functionality of the component, add a TTable, TDatasource, TDBNavigator 
and TDBPanel to a form. Set the TTable DatabaseName and Tablename to 'DBDemos' and 
'BioLife', and the Active property to True. Set the TDatasource Dataset property to 
Table1. Set the TDBNavigator and TDBPanel DataSource property to Datasource1. The 
TDBpanel DataField name should be set as 'Common_Name'. Run the application and use 
the navigator to move between records to demonstrate the TDBPanel's ability to 
detect the change in data and display the appropriate field value.

Full source listing:

73  unit Mydbp;
75  interface
77  uses
78    SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
79    Controls, Forms, Dialogs, ExtCtrls, DB, DBTables;
81  type
82    TDBPanel = class(TCustomPanel)
83    private
84      FDataLink: TFieldDataLink;
85      function GetDataField: string;
86      function GetDataSource: TDataSource;
87      procedure SetDataField(const Value: string);
88      procedure SetDataSource(Value: TDataSource);
89      procedure DataChange(Sender: TObject);
91    public
92      constructor Create(AOwner: TComponent); override;
93      destructor Destroy; override;
94    published
95      property DataField: string read GetDataField write SetDataField;
96      property DataSource: TdataSource read GetDataSource write SetDataSource;
97    end;
99  procedure register;
101 implementation
103 procedure register;
104 begin
105   RegisterComponents('Samples', [TDBPanel]);
106 end;
108 function TDBPanel.GetDataField: string;
109 begin
110   Result := FDataLink.FieldName;
111 end;
113 function TDBPanel.GetDataSource: TDataSource;
114 begin
115   Result := FDataLink.DataSource;
116 end;
118 procedure TDBPanel.SetDataField(const Value: string);
119 begin
120   FDataLink.FieldName := Value;
121 end;
123 procedure TDBPanel.SetDataSource(Value: TDataSource);
124 begin
125   FDataLink.DataSource := Value;
126 end;
128 procedure TDBPanel.DataChange(Sender: TObject);
129 begin
130   if FDataLink.Field = nil then
131     Caption := ''
132   else
133     Caption := FDataLink.Field.AsString;
134 end;
136 constructor TDBPanel.Create(AOwner: TComponent);
137 begin
138   inherited Create(AOwner);
139   FDataLink := TFieldDataLink.Create;
140   FDataLink.OnDataChange := DataChange;
141 end;
143 destructor TDBPanel.Destroy;
144 begin
145   FDataLink.Free;
146   FDataLink.OnDataChange := nil;
147   inherited Destroy;
148 end;
150 end.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC