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:
[crayon-67500918b40bf333579908/]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:
[crayon-67500918b40c7726803352/]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.
[crayon-67500918b40e6254906877/]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.
[crayon-67500918b40e8301264365/]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.
Here you can find the full code of the TfrxDBGridEh component.
Note that this article was translated by the Embarcadero Editor from the Michael’s original Russian version which can be found here