How to make custom ToolBar in report preview
Most report generators have a report-viewing mode with a toolbar from which you can make various manipulations of the report. For example, flipping through pages, printing, exporting and more. However, sometimes there is not enough buttons with any functionality. This can be a quick export to some format, or sending a report to a review, or maybe you might want to do a mass mailing of the report. Either way, the standard toolbar FastReport.Net does not allow you to add your custom features. But the developers have provided the possibility of creating their own preview mode. And it is great, not everyone likes the standard Preview. Many would love to remove some unnecessary buttons, but would add their own “features”. Therefore, let's look at how to quickly and easily make your Preview for reports. All features from the standard preview mode are available to us in the FastReport.dll library.
Let's take a look at this example. A typical Windows Forms application. We are connecting FastReport.dll library to it. On the form we add the toolbar component ToolStrip and PreviewControl - a standard report viewer. This component already has its own toolbar, but it's easy to hide in the toolbar properties.
On the toolbar, we created buttons with the necessary functionality:
Open the report, print, export, flipping pages and a special button with its functionality. It will keep a report in csv format in the specified folder. Such a QuickSave. Suppose we often use this particular functionality, so we moved it to a separate button. Now let's look at the application code:
Download report button opens a standard File Open dialog window. Then we upload the database to a data source, upload the selected report template in the report object, register it as a source of data to collect and display the report in the component Preview.
Export/ save button actually has a drop-down list with a variety of export formats. First, I clear the drop-down list, and create a list of objects for export. Loading the list of all registered objects.
Then, I add the first element to the list of exports - exports native format, i.e. fpx format. All the other available formats are added to the list in a loop. Some of the exports have picture. Each element of the list is assigned to item_Click event that handles pressing the item.
The event handler item_Click is exporting the report, which is currently displayed in the Preview component.
Print button opens PrintDoc dialog window.
“First” button shows the first page of the report.
“Prev” button shows the previous page of the report.
“Next” button shows the next page of the report.
And finally “Last” button shows the last page of the report.
The text field between buttons shows the number of the current page. Changing this value you can switch between the report pages.
But if to switch between pages using buttons the page number in the text field will change as well. According to page change event in Preview component.
In conclusion - our custom button. Its functionality is taken from nowhere, just for demonstration. It saves a report in csv format. Thus you can make a convenient report viewer for yourself or the customer, providing it with the necessary functions.
Dream on additional features in the Preview can be infinite, the main thing that the developers have given us this opportunity.
And if you want to use your preview component when displaying reports from the application code, we use the following code:
Here we override the view component of the report. Telling truth we had to make previewControl1 public.
That's all. I hope you take advantage of this great opportunity, how to make a custom viewer reports.