1
votes

So I have this problem where I want to capture the print button in print report button in crystal report. How to do this?

The user will click the Print Report button in crystal report as shown in the first image, Print page will pop up as shown in the second image

enter image description here

enter image description here

So when the user will click the print button, I want to do something like put a message box and run a query in my vb project. How to capture the 'Print' button?

3
Did you find out how to do this using c#?ivias

3 Answers

2
votes

you can try this solution:

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
0
votes

You can do this ! Requeirements:C# (but you can change to VB.NET), Visual Studio 2015, WPF platform Where: [GenericReportViewer] that's it your component crystal report

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 

  }
0
votes

If you don't want to add a custom button to the crviewer toolstrip, and just want to capture the native print button click event, you can try this:

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

Note that item.AccessibleName is culture dependent