I have a large number of forms that need to be printed. I have added a button to each form that can trigger the printing of the form (in PDF format). Rather than repeat the code for every form, I want to create a common procedure that can be called when the button is selected. My code below creates errors!
Any help would be gratefully received.
What I have tried:
In my application I have a number of forms that I wish to print. So on each form I have a button to create the print.
The code behind the button is as follows;
Private Sub PDFTeamBut_Click(sender As Object, e As EventArgs) Handles PDFTeamBut.Click
PDFTeamBut.Visible = False
PrtTSBut.Visible = False
Array.ForEach(Me.Controls.OfType(Of TextBox).ToArray, Sub(tb As TextBox) tb.DeselectAll())
PrintForm1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
PrintForm1.PrintAction = Printing.PrintAction.PrintToPrinter
If PrintForm1.PrinterSettings.IsValid Then
PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("PaperA4", 830, 1170)
PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = False
PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(3.2, 3.2, 1.5, 14.5)
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
PDFTeamBut.Visible = True
PrtTSBut.Visible = True
Me.Hide()'Hides form to be printed
FormName.InputForm.Show()'Returns to input form
End Sub
I want to replace the code by calling up a Public Sub Procedure; such as
Private Sub PDFTeamBut_Click(sender As Object, e As EventArgs) Handles PDFTeamBut.Click
CreatePDF(NameOfForm)
End Sub
With a Public Sub Procedure ;
Public Class PDFPrint
Public Sub CreatePDF(ByVal FormName As Form)
'Creates a PDF file of the named form
FormName.PDFTeamBut.Visible = False
FormName.PrtTSBut.Visible = False
Array.ForEach(FormName.Controls.OfType(Of TextBox).ToArray, Sub(tb As TextBox) tb.DeselectAll())
FormName.PrintForm1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
FormName.PrintForm1.PrintAction = Printing.PrintAction.PrintToPrinter
If FormName.PrintForm1.PrinterSettings.IsValid Then
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("PaperA4", 830, 1170)
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = False
FormName.PrintForm1.PrinterSettings.DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(3.2, 3.2, 1.5, 14.5)
FormName.PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
FormName.PDFTeamBut.Visible = True
FormName.PrtTSBut.Visible = True
FormName.Hide()'Hides form to be printed
InputForm.Show()'Returns to input form
End Sub
End Class
But I get the following coding errors such as;
Error BC30456 'PDFTeamBut' is not a member of 'Form'.
and
Error BC30456 'PrintForm1' is not a member of 'Form'.