How to create your own component for FastReport .NET

2021-05-14

The FastReport .NET report generator offers broad opportunities for customization, that is, configuring for your own needs. From the user application code, you may create your own components of the report generator, using the FastReport libraries. Thus, you may create your own report designer and its viewer.

Also, you may change dialog windows in the report designer, for example, a window of file saving.

However, the truly limitless opportunities for customization are provided by the source code of the report generator. You may reconfigure current functions for your needs or add new ones. Today, we will speak about how to add your own control element to the toolbox in the report designer.

Let us assume that you frequently use a text object with certain settings in your reports. It would be convenient to have a configured object “text” with a certain font or color highlighting, not to set these properties manually every time.

You will just have to create your own object! For that, it is sufficient to inherit your object from the existing one, broadening its functions or creating new ones.

To create your own component for the report, you need the source code of the report generator FastReport .NET. Let us consider the case of creating your own object based on the Text object. For that, add a new class to the root of the FastReport project:

 public class CustomerTextObject : TextObject
 {
 public override void OnBeforeInsert(int flags)
 {
 base.OnBeforeInsert(flags);
 Border.Lines = BorderLines.All;
 }
 }

In this class, we have redefined the event before inserting the object, in which we set the default value for the object boundaries. Thus, our text object will have incorporated boundaries from the very beginning. If you need to set any other properties of the object – no problem. Let us set the color:

public override void OnBeforeInsert(int flags)
 {
 base.OnBeforeInsert(flags);
 Border.Lines = BorderLines.All;
 TextColor = System.Drawing.Color.Gray;
 }

If you want to create an object with your own functionality, inherit from the base class ReportComponentBase. But in that case, besides the object properties and behavior, you will have to make rendering – the Draw() method. Then you evoke base.Draw() in it and add drawing of the object.

Let us consider the example of rendering a Text object:

public override void Draw(FRPaintEventArgs e)
 {
 base.Draw(e);
 DrawText(e);
 DrawMarkers(e);
 Border.Draw(e, new RectangleF(AbsLeft, AbsTop, Width, Height));
 DrawDesign(e);
 }

As you can see, several methods at a time are evoked here: for rendering the text, the markers, the boundaries, and the editor.

After creating your object, you should register it so that it appears in the toolbox in the report designer. To do that, use the method FastReport.Utils.RegisteredObjects.Add() in class AssemblyInitializer.cs.

The example of registering the object we have created:

RegisteredObjects.Add(typeof(CustomerTextObject), “ReportPage”, 120, 1);

All parameters are in order: the name of the registered object, the name of the object of a report page, the identifier of the object icon, the ordinal number of the object in the toolbox. The icons are stored in resources. You may add your image in the bmp format into the resources or send it directly to the method parameter instead of the icon identifier.

Now you can compile the solution and start the report designer. One more control appears in the toolbox:

The object in the report designer

We used the icon of the Text object, that is why it looks the same. Now we add it to the report page and enter the text:

The “Text” object

As we can see, both the frames and the text color correspond to our settings in the code. We have created a “custom” object “Text” with preset boundaries and text color. With this method, you can configure any existing object in the report or create your own one.

November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
October 28, 2024

How to Set Up WSL 2 for Working with FastReport and FastCube

In this article, we will explore how to set up WSL 2 for working with FastReport and FastCube components in Lazarus for Linux.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.