ActiveReports includes a control to view report output in custom preview forms. The viewer allows developers to modify the toolbars or add custom menu commands to the preview form.
To create a basic preview form
- Open a new Windows Forms project in Visual Studio and size the form according to your needs.
- From the Visual Studio toolbox, drag the Viewer control onto the form. If you have not added the viewer to the toolbox, see Adding ActiveReports Controls for more information.
- In the Properties window, set the Dock property to Fill.
- From the Project menu, select Add New Item.
- Select ActiveReports 6 File and click the Add button.
- Double-click in the title bar of the form to create a Form Load event.
- Add the following code to run the report and display the resulting document in the viewer.
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Form Load event. |
Copy Code |
Dim rpt as new NewActiveReport1
rpt.Run()
Viewer1.Document = rpt.Document
|
To write the code in C#
C# code. Paste INSIDE the Form Load event. |
Copy Code |
ActiveReport1 rpt = new ActiveReport1();
rpt.Run();
viewer1.Document = rpt.Document;
|
- Press F5 to run the project.
To use split windows on the viewer control
- With the project running, click the splitter control and drag downward.
- When the viewer is split into two sections, report layouts can be examined and report pages can be compared easily.
To add a custom print button to the viewer control
- Add a second Windows Form to the project created above and name it frmPrintDlg.
- Add a label to frmPrintDlg and change the Text property to This is the custom print dialog.
- Add a button to frmPrintDlg and change the Text property to OK.
- Back on the viewer form, double-click the title bar of the form to create a Form Load event.
- Add the following code to the Form Load event to remove the default print button and add your own.
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Form Load event. |
Copy Code |
' Remove the default print button
Me.Viewer1.Toolbar.Tools.RemoveAt(2)
' Create and add the custom button
Dim btn As New DataDynamics.ActiveReports.Toolbar.Button()
btn.Caption = "MyPrint"
btn.ToolTip = "Custom Print Button"
btn.ImageIndex = 1
btn.ButtonStyle = DataDynamics.ActiveReports.Toolbar.ButtonStyle.TextAndIcon
btn.Id = 333
Me.Viewer1.Toolbar.Tools.Insert(2, btn) |
To write the code in C#
C# code. Paste INSIDE the Form Load event. |
Copy Code |
// Remove the default printer button
this.viewer1.Toolbar.Tools.RemoveAt(2);
// Create and add the custom button
DataDynamics.ActiveReports.Toolbar.Button btn = new DataDynamics.ActiveReports.Toolbar.Button();
btn.Caption = "MyPrint";
btn.ToolTip = "Custom Print Button";
btn.ImageIndex = 1;
btn.ButtonStyle = DataDynamics.ActiveReports.Toolbar.ButtonStyle.TextAndIcon;
btn.Id = 333;
this.viewer1.Toolbar.Tools.Insert(2,btn); |
- Add the following code to the Viewer ToolClick event to display frmPrintDlg when the custom print button is clicked.
To write the code in Visual Basic.NET
- At the top left of the code view of the viewer form, click the drop-down arrow and select (YourReportName Events).
- At the top right of the code window, click the drop-down arrow and select ToolClick. This creates an event-handling method for the viewer's ToolClick event.
- Add code to the handler to display frmPrintDlg when the custom print button is clicked.
Visual Basic.NET code. Paste INSIDE the Viewer ToolClick event. |
Copy Code |
' Capture the new tool's click to show the dialog
If e.Tool.Id = 333 Then
Dim dlg As New frmPrintDlg()
dlg.ShowDialog(Me)
End If |
To write the code in C#
- Click the viewer on the form to select it.
- Click the events icon in the Properties Window to display available events for the viewer.
- Double-click ToolClick. This creates an event-handling method for the viewer's ToolClick event.
- Add code to the handler to display frmPrintDlg when the custom print button is clicked.
C# code. Paste INSIDE the Viewer ToolClick event. |
Copy Code |
// Capture the new tool's click to show the dialog
if(e.Tool.Id == 333)
{
frmPrintDlg dlg = new frmPrintDlg();
dlg.ShowDialog(this);
}
|
- Press F5 to run the project and see the custom MyPrint button on the viewer.
See Also