October 30, 2024
Using Styles When Creating Reports in FastReport VCL
In FastReport VCL, a style refers to a set of visual parameters for a component, specifically: font, fill, and border. Right in the report designer, you can create a style that contains only font information, or just the border, or a combination of fill and border. You can use all of these parameters simultaneously or alternate them depending on your needs.
Styles are convenient for mass changes to the appearance of components. This helps when adjusting several parameters so that you don't have to repeatedly set the same properties manually. Additionally, by changing style parameters, you can quickly alter the appearance of a report. You can save a completed style library in a separate file for use in configuring multiple reports.
In FastReport VCL, styles can be combined into a ''StyleSheet''. The collection of style sheets is referred to as a "Style Table" (or "Style Book"). Each style exists within each style sheet, and in each style sheet, the same style can have different parameters. This allows for quick changes to the report's appearance simply by switching the current style sheet. This can be useful, for example, if the same report needs to be used in different operating systems with different fonts. Thus, the necessary styles can be set just by switching the style sheet.
It can be considered that in FastReport VCL, each report has at least one style sheet named "Default."
Style Control Elements
To manage and work with styles and style tables, there is a “Styles” toolbar.
Controls the use of style tables: This includes a submenu with two options: "Designer Style Book" — Allows the use of style tables in the report designer."Preview Style Book" — Allows the use of style tables in the preview window (to change the current style table).
Enables style editing mode directly in the report.
Adds a new style sheet.
Responsible for selecting the active style sheet, as well as changing the name of the style sheet.
Deletes the currently selected style sheet.
Adds a new style.
Allows selection of a style for the currently highlighted component, and enables changes to the name of the current style.
Deletes the current style.
Additionally, there is a form available for editing and managing styles, which can be accessed from the menu “Report” -> “Styles.”
The toolbar buttons on this form have the same functions as those on the “Styles” toolbar. Additionally, it allows you to save and load style tables from disk.
On the left side of the form, there is a list of styles available in the report. When any style is selected, its parameters can be edited. Below this, there is a sample showing how the style is applied to a report element.
In the center of the form, there are 3 buttons that allow you to edit the components of the style. Next to the buttons, there are 3 checkboxes where you can indicate whether the style contains a specific component — background color, font, or border. The use of each component of the style is either allowed or prohibited across all style sheets at the same time.
Working With Styles
To set style properties, there is a special form accessible from the menu “Report -> Styles…” (screenshot above). This form allows you to add and remove styles in the report, rename them, and set their properties. You can also save and load them from style files.
You can assign a style to a selected report component using the ComboBox on the styles panel or through the “Style” property in the Object Inspector. If you set a report component style to “No Style” or remove the style name in the Object Inspector, the appearance of the component will be retained. However, after these actions, the component will no longer depend on the style.
You can also switch the report designer to style editing mode using the button on the styles panel (Edit Styles Mode).
In this mode, two buttons appear on either side of the ComboBox with the style names, allowing you to add and remove styles using these buttons. When a style is created using the button on the styles panel, the new style inherits the properties of the currently selected component. When a style is deleted, the formatting of the report components is preserved. However, this formatting will now be stored in the component itself rather than in the style.
Each user has the ability to edit styles directly in the report. If you edit the visual properties of an element that is assigned a style, you are essentially editing the properties of that style. This behavior can be observed directly in the designer. When changing the parameters of one component, all components that share the same style will also change.
Working With Style Tables
To work with style tables, you first need to enable them. This is done by clicking the “Style Book Mode” button and selecting either the “Design Style Book” or “Preview Style Book” option. After this, the buttons “Add Style Sheet”, “Delete Style Sheet”, and the ComboBox with style table names become active. You can add, delete, and rename style tables, as well as make any one of them active.
When a style table is activated, all styles change their state to match that of the current style table.
When creating a new style table, the data for it is taken from the currently active style table. With the “Preview Style Book” option active, changing style sheets is available in the preview mode. A ComboBox will appear on the right side of the toolbar, where you can select the desired style sheet, and it will be immediately applied to the report.
Saving and Reading Styles
Let's return to the style configuration window. In this window, there are "Save" and "Load" buttons. When clicked, these buttons open file dialogs for reading and writing, allowing you to save and upload the style table in the *.fs3 format. Regardless of the working mode with styles, all style tables present in the report are saved to the file.
Typically, the end user should not edit the report; they only have the option to preview it. Therefore, the programmer should be able to set the current style of the report in the application code. This can be done as follows:
begin //loading the report frxReport1.LoadFromFile(DefPath+'main.fr3'); //loading styles frxReport1.Styles.LoadFromFile(DefPath+'main.fs3'); // to manage style tables, frxReport1.Styles.StyleBookMode should not be equal to [] frxReport1.Styles.StyleBookMode:=[fsbDesigner]; //setting the desired style frxReport1.Styles.ActiveStyleSheet:='MyStyleSheet'; //applying the style to the report frxReport1.Styles.Apply; frxReport1.ShowReport();end;
More details on working with styles are described in the programmer's guide.
Style Tables and Conditional Formatting
Conditional formatting should be used with caution alongside style tables. If the “Preview Style Book” mode is active, changing the style will result in the loss of conditional formatting. However, formatting elements that are not used by the style can be changed without issues.
In summary, working with styles and style sheets is not difficult at all. This functionality significantly simplifies and speeds up the report creation process. It allows you to create groups of reports with the same formatting style much more quickly.