ActiveReports中提供不同平台的报表浏览器来加载报表,而不同平台的报表浏览器功能也不一致,今天我们来学习如何定制Winforms Viewer控件。
预览效果:
核心代码:
C#
// C#
private ToolStripButton tsbPrint = new System.Windows.Forms.ToolStripButton();
private void frmViewer_Load(object sender, EventArgs e)
{
System.Windows.Forms.ToolStrip toolStrip;
System.Windows.Forms.ToolStripItem orgItem;
System.Windows.Forms.ToolStripButton orgBtn = null;
// Get the image from the standard Print button.
toolStrip = this.viewer1.Toolbar.ToolStrip;
orgItem = toolStrip.Items[2];
if (orgItem is System.Windows.Forms.ToolStripButton)
{
orgBtn = (System.Windows.Forms.ToolStripButton)orgItem;
}
// Delete the standard Print button.
toolStrip.Items.RemoveAt(2);
// Create a custom button to use in place of the standard Print button.
tsbPrint.Text = “HQ Print”;
tsbPrint.ToolTipText = “Print to the company headquarters main printer”;
tsbPrint.Image = orgBtn.Image;
tsbPrint.Enabled = false;
// Set the event handler. (viewer.LoadCompleted can be set from the designer)
tsbPrint.Click += this.PrintButtonClick;
viewer1.LoadCompleted += this.viewer1_LoadCompleted;
// Add the custom button to the toolbar.
toolStrip.Items.Insert(2, tsbPrint);
// Instantiate the report.
GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();
// Load a report definition file.
rpt.Load(new System.IO.FileInfo(@”....PageReport1.rdlx”));
GrapeCity.ActiveReports.Document.PageDocument pageDocument =
new GrapeCity.ActiveReports.Document.PageDocument(rpt);
// Load the report into the viewer.
viewer1.LoadDocument(pageDocument);
}
// The event to call when the report is loaded into the Viewer.
private void viewer1_LoadCompleted(object sender, EventArgs e)
{
// Enable the custom button.
tsbPrint.Enabled = true;
}
// The event to call when the custom button is clicked.
private void PrintButtonClick(System.Object sender, System.EventArgs e)
{
// Perform print processing.
this.viewer1.Print(true, true, false);
}
VB.NET
‘ VB.NET
Private tsbPrint As New System.Windows.Forms.ToolStripButton
Private Sub frmViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim toolStrip As System.Windows.Forms.ToolStrip
Dim orgBtn As System.Windows.Forms.ToolStripButton = Nothing
Dim orgItem As System.Windows.Forms.ToolStripItem
‘ Get the image from the standard Print button.
toolStrip = Me.Viewer1.Toolbar.ToolStrip
orgItem = toolStrip.Items(2)
If TypeOf orgItem Is System.Windows.Forms.ToolStripButton Then
orgBtn = CType(orgItem, ToolStripButton)
End If
‘ Delete the standard Print button.
toolStrip.Items.RemoveAt(2)
‘ Create a custom button to use in place of the standard Print button.
tsbPrint.Text = “HQ Print”
tsbPrint.ToolTipText = “Print to the company headquarters main printer”
tsbPrint.Image = orgBtn.Image
tsbPrint.Enabled = False
‘ Set the event handler (Viewer.LoadCompleted can also be set in the designer)
AddHandler tsbPrint.Click, AddressOf Me.PrintButtonClick
AddHandler Viewer1.LoadCompleted, AddressOf Me.Viewer1_LoadCompleted
‘ Add the custom button to the toolbar.
toolStrip.Items.Insert(2, tsbPrint)
‘ Instantiate the report.
Dim rpt As New GrapeCity.ActiveReports.PageReport()
‘ Load a report definition file.
rpt.Load(New System.IO.FileInfo(“....PageReport1.rdlx”))
Dim pageDocument As New GrapeCity.ActiveReports.Document.PageDocument(rpt)
‘ Load the report into the viewer.
viewer1.LoadDocument(pageDocument)
End Sub
‘ The event to call when the report loads in the Viewer.
Private Sub Viewer1_LoadCompleted(sender As Object, e As EventArgs)
‘ Enable the custom button.
tsbPrint.Enabled = True
End Sub
‘ The event to call when the custom button is clicked.
Private Sub PrintButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
‘ Perform print processing.
Me.Viewer1.Print(True, True, False)
End Sub