When creating a report, it is important not only to make a nice printable form that will display the data the user needs but also a convenient pre-print dialog, where the user can set the parameters to generate the report.
The FastReport preprint dialog already contains a number of components, for example, Label, Edit, Button, Memo. But sometimes this is not enough and you have to create your own dialog components for FastReport. In fact, they are analogs to the usual Delphi components that can be used in FastReport dialog forms. This article will talk about creating this component using IDE Software: Delphi.
For more information, we recommend reading the FastScript documentation and the FastReport VCL manuals for developers and software specialists.
To display data from the database, this article uses the DBGridEh component from the EhLib VCL library L, which will be integrated into the FastReport dialog. Delphi 10.4.2 was used as a development environment, but everything described below will be suitable for any other Delphi starting from version 7 (adjusted for the version).
Let’s create a new package (dpk) and name it frxDBGridEh27. Add to it a new file frxDBGridEhControl.pas, which will serve as the basis for our grid.
We will declare a component in this file for registration in Delphi:
TfrxDBGridEhDialogControl = class(TComponent);
We begin to describe the TfrxDBGridEh component, which will be created from the base dialog class TfrxDialogControl. In fact, we will re-declare the properties of the base DBGridEh component into a component to use in FastReport. You can re-declare not 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 rest of the methods in this section are needed to read/write the DBGridEh properties, which we will set in frxDBGridEh in the FastReport dialog. These methods are some kind of intermediaries between frxDBGridEh and DBGridEh.
The protected section contains three system overridden methods for handling events occurring in the FastReport designer: deleting a data source that is already connected to the grid, setting methods for reading/writing grid columns to the fr3 report template file, and processing in order to specify the component name.
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. Among other things, you need to create a basic (internal) DBGridEh inside the constructor, a data connection interface (DataSource) for connecting to the grid, and event handlers.
We set the 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 of this section are needed to programmatically use 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 object inspector of the FastReport designer and which can also be set in the report script.
Note the initialization section, which states:
- description of grid event handlers for different programming languages of the report script;
- registration of the grid in the palette of FastReport components.
Now let’s add another frxDBGridEhColumnsEditor module to our package (code plus form). This module uses the capabilities of FastReport for visual work with collections in the designer. At this stage, we create a grid column editor similar to the Delphi column editor. In the initialization section, we register this editor in the FastReport environment for visual editing of a set of frxDBGridEh columns.
This module has two classes: TfrxDBGridEhColumnsEditorForm — the form of the column editor with the necessary functionality and TfrxDBGridEhEditor for connecting (calling) our editor from the FastReport designer.
Next, we add another frxDBGridEhEditor file to the package where we will place the editors of the grid properties. First of all, let’s add a property for editing columns by linking Columns: TDBGridColumnsEh to our editor.
Now we create an editor for selecting a data source. We made the SetDataSet and SetDataSetName methods more universal than, for example, in the standard frxDBLookupComboBox, that’s why we can use both TDataSet heirs (Delphi) and TfrxDBDataset heirs (FastReport) as a data source. In the end, we add an editor to select a field for a grid column.
The last module we add to our package is the frxDBGridEhRTTI file, where we implement the TFunctions = class (TfsRTTIModule), inside which we add classes, methods, and properties for the report script (FastScript) that we want to programmatically call from it (for example, methods for program addition/removal of columns and for saving the state of grid columns).
The final step is to create the frxDBGridEh.dcr file with an icon of our component. This is necessary to display Delphi components and the frxDBGridEh.res file in the palette with an icon of our component for displaying the FastReport component in the palette.
For more blog posts about FastReport click here for a dynamic search: https://blogs.embarcadero.com/?s=FastReport
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition