Example of using the “OnBeforePrint” event
|Top Previous Next|
To demonstrate this event create a report representing a list of clients. This report will include only those companies whose name begins with the letter “A”.
Let's create a new project in Delphi, place “TTable”, “TfrxDBDataSet” and “TfrxReport” components on the form and set these properties:
DatabaseName = 'DBDEMOS'
TableName = 'customer.db'
DataSet = Table1
UserName = 'Customers'
Open the report designer and create a report like this:
Select the data band and switch to the “Events” tab in the object inspector:
To create an “OnBeforePrint” event handler (which is the most appropriate for us) double-click on the blank field to the right of the event’s name:
This adds a blank handler to the script and the designer switches to the “Code” tab.
All that is needed now is to type the following code in the handler’s body:
if Copy(<Customers."Company">, 1, 1) = 'A' then
MasterData1.Visible := True
MasterData1.Visible := False;
if (Copy(<Customers."Company">, 1, 1) == "A")
MasterData1.Visible = true;
MasterData1.Visible = false;
Run the report and make sure that the script works correctly:
Let's explain several things. One handler can be assigned to the events of more than one object - the “Sender” parameter shows which object has initiated the event. To assign an existing handler to an event, either type it directly into the object inspector, or select it from the drop-down list:
A link to a handler can easily be deleted - select the assigned handler in the object inspector and press the “Delete” key.