ActiveReports中自定义Winforms的Viewer 工具栏

发布时间:2016/09/27 00:09 发布者:lenka.guo

返回博客中心

ActiveReports中提供不同平台的报表浏览器来加载报表,而不同平台的报表浏览器功能也不一致,今天我们来学习如何定制Winforms Viewer控件。

预览效果:

Shows a run-time view of the custom print button showing the tooltip text.

 

核心代码:

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

 

了解ActiveReports产品更多特性:

/developer/activereports

下载产品体验产品功能:

/download/?pid=16


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网