
A Non Visual Component to iterate over the records in a TTable
In a non object orientated language, a standard code pattern is the
Process a File loop. The standard code looks a little like this:
FTable1.First;
while not Table1.EOF do
begin
{ Process the record - application specific code}
FTable1.Next;
Application.ProcessMessages;
end;
However, as this is Delphi, I decided to develop a component to provide
the basic structure. Basically, I like to reuse code wherever possible
- repeating this code through-out a program seemed wasteful. Another, more
important reason, is that should the above skeleton code need to change,
then the changes are restricted to one module rather than spread throughout
many different components and programs. For example, what if after reading
all the records in the TTable, you wanted to restore the file to its original
position. Then you would need to add code to create and free TBookmarks:
FTable1Bookmark := FTable1.GetBookmark;
try
FTable1.First;
while not Table1.EOF do
begin
{ Process the record - application
specific code}
FTable1.Next;
Application.ProcessMessages;
end;
FTable1.GotoBookmark(FTable1Bookmark);
finally
FTable1.FreeBookmark(FTable1Bookmark);
end;
The you might have to set up Ranges on the dataset based on the MasterSource
and MasterFields. Similarly, you find that the display flickers because
the controls are Enabled so code must be added to disable them then re-Enable
them after the loop. All of a sudden our simple loop structure has become
very complicated. Each time you need more functionality, all the code written
must be checked and if necessary changed and then retested. The odds of
getting the code changes correct the first time are not good. Also, with
each set of new changes the original code becomes more cluttered and difficult
to understand.
But if the code is encapsulated within a component, the code changes
are almost a pleasure to make. Also, the component is located on the Component
Palette and more likely to be used than if it was buried in some other
project in some other directory.
TProcessTable Component
This component can be used to iterate over all or some of the records in
a TTable. I have modified the MASTAPP demo from Borland to illustrate how
this component is used. The component addresses many of the issues raised
above such as disabling controls and restoring the position within the
TTable.
TProcessDetailTable
This component is designed to iterate over the Detail records in a Master-Detail
relationship. The program that I developed to keep my Tennis Club records,
uses this component to search the Receipts database. Originally, I simply
matched the index fields of the master and detail tables without considering
the order of the fields in either table. Upon reflection, I decided this
was wrong and decided to use the MasterSource and MasterField properties.
The tricky part is that the component had to cope with either or both TTables
being inactive (closed).
Properties
Events
Register Procedure
The Register component is a very important for any component that is to
be installed on the component palette. Enough said.
Notification Method
I can only echo what Ray Konopka said in his book Developing Custom
Delphi Components Providing a Notification method is especially
important for design-time operation (p117). The Delphi Component Writer's
Guide states The Notification method notifies the component that the
component specified by A Component is about to be inserted or removed,
as specified by Operation. By default, components pass along the notification
to their owned components, if any. A component can, if needed, act on the
notification that a component is being inserted or removed. In particular,
if a component has object fields or properties that contains references
to other components, it might check the notifications of component removals
and invalidate those references as needed. Simply, if your refer to
other components in the properties of your component, the Notification
Method must update your references to those components as they change otherwise
a GPF occurs.
Using Resource Workshop
The Resource Workshop (mine came with Turbo Pascal for Windows) can be
used to open any .DCL file. A DCL is the Delphi Component Library but is
actually a DLL. By entering the appropriate file name and setting the file
type to DLL you can examine the Windows resources in the DCL. You can copy
the component bit maps and paste them into another .RES file and change
them to create a bit map for your component.
After creating the DCR it is necessary to copy it renaming the extension
to DCR (for Delphi Component Resource)
Restrictions
The component only operates with TTable components. TQuery objects are
not supported.
Last Revised: 7 February 1999
Copyright © 1997
gra...@pcug.org.au