捕获打印报告'打印'水晶报告中的按钮

时间:2018-05-31 08:27:46

标签: vb.net crystal-reports

所以我有这个问题,我想在水晶报告中捕获打印报告按钮中的打印按钮。怎么做?

用户将点击水晶报告中的“打印报告”按钮,如第一张图片所示,将弹出打印页面,如第二张图片所示

enter image description here

enter image description here

因此,当用户单击打印按钮时,我想要做一些事情,比如放一个消息框并在我的vb项目中运行查询。如何捕捉'打印'按钮?

3 个答案:

答案 0 :(得分:2)

你可以尝试这个解决方案:

Private Sub Frm_stampa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     ' Hide default button
    crv_stampa.ShowPrintButton = False

    ' New print button
    For Each ctrl As Control In crv_stampa.Controls
        If TypeOf ctrl Is Windows.Forms.ToolStrip Then
            Dim btnNew As New ToolStripButton
            btnNew.Text = "Print"
            btnNew.ToolTipText = "Print"
            btnNew.Image = My.Resources.stampa
            btnNew.DisplayStyle = ToolStripItemDisplayStyle.Image

            CType(ctrl, ToolStrip).Items.Insert(0, btnNew)

            AddHandler btnNew.Click, AddressOf tsItem_Click
        End If
    Next
    ' ---------------------------------------------
End Sub



Private Sub tsItem_Click(sender As System.Object, e As System.EventArgs)

    ' Put your code here, before print

    Dim PrintDialog As New PrintDialog()

    If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

        rpt.PrintOptions.PrinterName = PrintDialog.PrinterSettings.PrinterName

        rpt.PrintToPrinter(PrintDialog.PrinterSettings.Copies, PrintDialog.PrinterSettings.Collate, PrintDialog.PrinterSettings.FromPage, PrintDialog.PrinterSettings.ToPage)

    End If

End Sub

答案 1 :(得分:0)

您可以执行此操作! 要求:C#(但您可以更改为VB.NET),Visual Studio 2015,WPF平台 其中:[GenericReportViewer]就是您的组件水晶报表

private void GenericReportViewer_Loaded(object sender, RoutedEventArgs e){
//where: GenericReportViewer thats it name your crystal report component
 System.Windows.Controls.Button button = GenericReportViewer.FindName("btnPrint") as 
 System.Windows.Controls.Button;
      button.Click += MyMethod;
  }

private void MyMethod(object sender, RoutedEventArgs e){

   //Your code here 

  }

答案 2 :(得分:0)

如果您不想在crviewer工具栏中添加自定义按钮,而只想捕获本机打印按钮单击事件,则可以尝试以下操作:

Private Sub frmViewReport_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
     For Each control In crViewer.Controls

            If TypeOf control Is ToolStrip Then

                For Each item In control.items
                    If item.AccessibleName = "Imprimir Relatório" Then
                        Dim prtButton As ToolStripButton = DirectCast(item, ToolStripButton)
                        AddHandler prtButton.Click, AddressOf dostuff
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
End Sub

Private Sub dostuff()
        MsgBox("Print button was pressed")
End Sub

请注意, item.AccessibleName 与区域性有关