When creating a report, it is important not only to make a beautiful printed form that will display the data necessary for the user. but also a convenient prepress dialog where the user can set the parameters for generating the report.
In the RAD Studio IDE Software, on the fastReport prepress dialog, there are already a number of components, for example, Label, Edit, Button, Memo. But sometimes this is not enough and you have to write your own dialog components for FastReport. In fact, they are analogs to ordinary Delphi components, which can be used in dialog forms FastReport. Accordingly, in this article, we will talk about writing such a component.
For more information, we recommend reading the official documentation
FastScript documentation: https://www.fast-report.com/en/product/fast-script/documentation/
FastScript VCL documentation: https://www.fast-report.com/en/product/fast-report-vcl/documentation/
To display data from the database, this article uses the DBGridEh component from the EhLib VCL library, which will be integrated into the FastReport dialog. Delphi 10.4.2 is used as the development environment, but everything described below will be suitable for any other Delphi starting from version 7 (adjusted for the version).
Create a new package (dpk) and give it the name frxDBGridEh27. We add a new file frxDBGridEhControl.pas, which will be the basis of our grid.
In this file, for registration in Delphi, we will declare the component:
TfrxDBGridEhDialogControl = class(TComponent);
We begin to describe the TfrxDBGridEh component, which we will create from the base dialog class TfrxDialogControl. Essentially, we are re-declaring the properties of the base DBGridEh component into a component for use in FastReport. You can not declare all properties, methods, event handlers, but only those that are needed for work.
In the private section, I recommend paying special attention to the following:
FDBGridEh :TDBGridEh; // The base component, the properties of which we will set and methods of which we will call
FDataSource :TDataSource; // Interface between grid and data source in FastReport
// Variables that will store the name of the event handlers
// These system procedures are needed to save and restore grid columns to the fr3 report template.
procedure ReadData(Reader: TReader);
procedure WriteData(Writer: TWriter);
// These three methods are needed to be able to connect a basic data source of the TfrxDBDataset (FastReport) or TDataSet (Delphi) type to the grid.
function GetDataSet: TDataSet;
procedure SetDataSet(const Value: TDataSet);
procedure SetDataSetName(const Value: String);
// This is the event handler code
procedure DoGetCellParams(Sender: TObject; Column: TColumnEh; AFont: TFont; var Background: TColor; State: TGridDrawState);
procedure DoTitleBtnClick(Sender: TObject; ACol: Integer; Column: TColumnEh);
procedure DoGetBtnParams(Sender: TObject; Column: TColumnEh; AFont: TFont; var Background: TColor; var SortMarker: TSortMarkerEh; IsDown: Boolean);
procedure DoDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
procedure DoColEnter(Sender: TObject);
procedure DoCellClick(Column: TColumnEh);
procedure DoColWidthsChanged(Sender: TObject);
The remaining methods in this section are needed to read/write the DBGridEh properties that we will set for frxDBGridEh in the FastReport dialog. These methods are some intermediaries between frxDBGridEh and DBGridEh.
The protected section contains three system overrides methods for handling events that occur in the FastReport designer: deleting a data source that is already connected to the grid, specifying methods for reading/writing grid columns to the fr3 report template file, and processing the component name setting.
procedure Notification(AComponent :TComponent; AOperation :TOperation); override;
procedure DefineProperties(Filer: TFiler); override;
procedure SetName(const AName: TComponentName); override;
The public section contains methods such as the constructor and destructor of our component. Inside the constructor, among other things, you need to create a basic (internal) DBGridEh, a data connection interface (DataSource) to connect to the grid and event handlers.
We set a description of our grid, which will be visible in the FastReport designer, as well as additional methods for saving the collection of grid columns as XML to the fr3 report template. The rest of the methods and properties in this section are needed to enable them to be used programmatically in the report script.
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class function GetDescription: String; override;
procedure WriteNestedProperties(Item: TfrxXmlItem; aAcenstor: TPersistent = nil); override;
function ReadNestedProperty(Item: TfrxXmlItem): Boolean; override;
The published section contains properties and event handlers that will be visible in the FastReport Designer Object Inspector and that can also be set in the report script.
Note the initialization section that specifies:
- Description of grid event handlers for different report script programming languages;
- Grid registration in the FastReport component palette.
Now let’s add another frxDBgridEhColumnsEditor module to our package (this is the code plus the form). This module uses FastReport’s visual work with collections in the designer. At this stage, we create a grid column editor similar to the column editor in Delphi. In the initialization section, register this editor in the FastReport environment to visually edit the frxDBGridEh column set.
There are two classes in this module: TfrxDBGridEhColumnsEditorForm – a form of the column editor with the necessary functionality and TfrxDBGridEhEditor to connect (call) our editor from the FastReport designer.
Next, we add another frxDBGridEhEditor file to the package in which we will place the grid property editors. First of all, we will add a property for editing columns by linking Columns :TDBGridColumnsEh with our editor.
Now create an editor to select the data source. And due to the fact that we have made the SetDataSet and SetDataSetName methods more universal than, for example, in the regular frxDBLookupComboBox, you can use both TDataSet (Delphi) and TfrxDBDataset (FastReport) heirs as a data source. Finally, add an editor to select a field for the grid column.
The last module in our package will add the file frxDBGridEhRTTI, where we implement the class TFunctions = class(TfsRTTIModule), inside which we will add for the report script (FastScript) classes, methods and properties that we want to programmatically call from it. For example, methods for programmatically adding/removing columns and for saving the state of grid columns.
The final step is to create a frxDBGridEh file.dcr with our component icon to display the Delphi component in the palette and a frxDBGridEh.res file with our component icon to display in the FastReport component palette.
Note that this article was translated by the Embarcadero Editor from the Michael’s original Russian version which can be found here