Edit: my orginal question was too confusing.
Public Class PrintResults
Public Sub ResultsToPS(ByVal lis As List(Of FileData), ByVal PSPathName As String)
Me.List = lis
'setup report writer
Dim rep As New Helper.Report
'create report to PS
rep.ToPS(PSPathName)
End Sub
Public Class Report
Public Sub New()
PrintDoc = New Printing.PrintDocument
End Sub
Public WithEvents PrintDoc As Printing.PrintDocument
Public PrintDocPrintPage As PrintDoc_PrintPage
Public Delegate Sub PrintDoc_PrintPage(ByVal sender As Object, _
ByVal e As Printing.PrintPageEventArgs) 'Handles PrintDoc.PrintPage
Public Function ToPS(ByVal PSPathName As String) As String
'fails
AddHandler Me.PrintDoc.PrintPage, AddressOf Me.PrintDocPrintPage
Me.PrintDoc.Print()
End Function
AddHandler has compiler error: AddressOf operand must be the name of a method. Is there any way to assign the handler to a delegate sub?
With some research I found it's not possible to use a delegate sub with AddressOf. So we need a different approach. I see it now that all I have to do is instantiate the PrintDoc in the New sub. Now I have a PrintDoc object for the AddHandler to use.
Public Class Report
Public Sub New()
PrintDoc = New Printing.PrintDocument
End Sub
We use the regular AddHandler with no delegates:
Public Class PrintResults
Public Sub ResultsToPS(ByVal lis As List(Of FileData), ByVal PSPathName As String)
Me.List = lis
'setup report writer
Dim rep As New Helper.Report
'PrintDoc is automatically instatiated so no object error
AddHandler rep.PrintDoc.PrintPage, AddressOf Me.PrintDoc_PrintPage
'create report to PS
rep.ToPS(PSPathName)
End Sub