Click to See Complete Forum and Search --> : PrintDocument.PrintPage override
ladyubik_sara
March 27th, 2009, 08:42 AM
I need to pass a parameter to the event PrintPage of the PrintDocument relative to my PrintPreviewDialog in some way.
I tought to inherit PrintDocument class adding to it a property with the parameter I need, creating MyPrintDocument class. Then, I would like to create MyPrintPreviewDialog class, in which I could override the Document property declaring it of MyPrintDocument type.
The problem is that the Document property is not overridable and if I declare it Shadows the PrintPageEvent doesn't raise.
Do you have a better idea? Thank you
jmedved
March 27th, 2009, 03:43 PM
You can pass anything inherited from PrintDocument (even your MyPrintDocument) as Document. In MyPrintPreviewDialog just cast (either TryCast, DirectCast or CType) to proper type.
P.S. Notice that there is Tag property on your PrintDocument. You can pass data through it also.
ladyubik_sara
March 30th, 2009, 08:42 AM
Hi Jmedved,
thank you for your answer. I tried the solution of casting, but probably I did something wrong, because the PrintPage Event doesn't fire. Obviously what I need to print is more than what I wirte with the following code, this is just an example.
This is the code:
Imports System.Drawing.Printing
Public Class SlimPrice_PrintDocument
Inherits PrintDocument
Public dimension As String
Public Sub New(ByVal dimension As String)
MyBase.New()
Me.dimension = dimension
End Sub
End Class
Public Class SlimPrice_PrintPreviewDialog
Inherits PrintPreviewDialog
Public Sub New(ByVal document As SlimPrice_PrintDocument)
MyBase.New()
Me.Document = TryCast(document, PrintDocument)
End Sub
End Class
Public Class Example
Public Sub PrintLabel(ByVal dimension As String)
Dim doc As SlimPrice_PrintDocument
Dim ppd As SlimPrice_PrintPreviewDialog
doc = PreparePage(dimension)
ppd = New SlimPrice_PrintPreviewDialog(doc)
ppd.Show()
End Sub
Public Function PreparePage(ByVal dimension As String) As SlimPrice_PrintDocument
Dim printDocument As SlimPrice_PrintDocument
printDocument = New SlimPrice_PrintDocument(dimension)
AddHandler printDocument.PrintPage, AddressOf Print_PrintPage
Return printDocument
End Function
Public Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
e.Graphics.DrawString(sender.dimension, New Font("Arial", 10), Brushes.Black, 10, 10)
e.HasMorePages = False
End Sub
End Class
I can find the Tag property of PrintDocument class. If I write doc.Tag the error is "Tag is not a member of PrintDocument ". Thank you so much.
jmedved
March 30th, 2009, 09:06 AM
Hi,
Can you send small working example of that?
It is difficult to see what is wrong if not all code is there.
ladyubik_sara
March 30th, 2009, 10:00 AM
Writing the working example I realized that the error was not in the inheritance, but in another class. Sorry and thanx for your help. It was very helpful!
jmedved
March 30th, 2009, 12:37 PM
That usually happens. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.