CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    3

    PrintDocument.PrintPage override

    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

  2. #2
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: PrintDocument.PrintPage override

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Re: PrintDocument.PrintPage override

    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:

    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.

  4. #4
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: PrintDocument.PrintPage override

    Hi,

    Can you send small working example of that?

    It is difficult to see what is wrong if not all code is there.

  5. #5
    Join Date
    Mar 2009
    Posts
    3

    Re: PrintDocument.PrintPage override

    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!

  6. #6
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: PrintDocument.PrintPage override

    That usually happens.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured