Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'.	
Posted
Updated 5-May-17 12:02pm
v3
Comments
[no name] 5-May-17 15:42pm    
Looks like a job for inheritance.
Dave the Golfer 5-May-17 15:55pm    
Do not understand!

1 solution

Since posting the problem I have found some information on the internet that made me change the following lines
FormName.PDFTeamBut.Visible = False
FormName.PrtTSBut.Visible = False

and
FormName.PDFTeamBut.Visible = True
FormName.PrtTSBut.Visible = True

to
FormName.Controls("PDFTeamBut").Visible = False
FormName.Controls("PrtTSBut").Visible = False

and
FormName.Controls("PDFTeamBut").Visible = True
FormName.Controls("PrtTSBut").Visible = True


This corrected the first error.
Still have a problem with the PrintForm1 error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900