locked
e.HasMorePages RRS feed

  • Question

  • Please help me.  I know this has got to be very, very simple.  But I'm missing something.  I simply want to print 2 pages.  This is a condensed version of what I'm doing.

     

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

     

    e.Graphics.DrawString("Hello", New Font ("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

     

    e.HasMorePages = True

     

    e.Graphics.DrawString("Hello Again", New Font ("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

     

    e.HasMorePages = False

     

    End Sub

     

    I'm wanting the first page to print "Hello" and the second page to print "Hello Again".  Obviously, when I run the program the page counter never stops and only prints the first page a million times.  I know that somehow I'm supposed to end the event handler, but I don't know how.  What do I do?

    Tuesday, August 21, 2007 12:35 AM

Answers

  • Thank you all so much for your help.  If I can't figure it out from here, I'll get some rope, duct tape and beer and have a self-hanging party. ha. Thanks again.  I will spend the night working on this and if I figure it out I will close this thread, and if not,  "I'll Be Back".

     

    Thanks again,

    Brad

    Wednesday, August 22, 2007 2:37 AM

All replies

  • The e.hasmorepages True/False is used

    when the 'end sub' of the printpage routine

    is reached.

     

    If it is true, the printpage routine is called again,

    otherwise it is not.

     

    E.hasmorepages has no effect in the middle of the

    printpage routine.

    Tuesday, August 21, 2007 12:44 AM
  • I appreciate your help Tall Dude.  So if I place the hasmorepages when the printpage routine is reached, how do I tell it to print the second page?

     

    Tuesday, August 21, 2007 12:57 AM
  • Maybe this bit of code will help:

     

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

    Dim linesPerPage As Single = 0

    Dim yPos As Single = 0

    Dim count As Integer = 0

    Dim leftMargin As Single = ev.MarginBounds.Left

    Dim topMargin As Single = ev.MarginBounds.Top

    Dim line As String = Nothing

    ' Calculate the number of lines per page.

    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Print each line of the file.

    While count < linesPerPage

    line = streamToPrint.ReadLine()

    If line Is Nothing Then

    Exit While

    End If

    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

    count += 1

    End While

    ' If more lines exist, print another page.

    If Not (line Is Nothing) Then

    ev.HasMorePages = True

    Else

    ev.HasMorePages = False

    End If

    End Sub

     

    You need to find out how many lines per page you will be printing. If the number of lines you have to print

    exceeds the available space on a single page, then ev.HasMorePages is True and PrintDocument will send the

    correct info to the printer to continue on to the next page(s), until ev.HasMorePages is False.

    Hope this gives you some ideas of what you need.

    james

    aka:Trucker

     

    Tuesday, August 21, 2007 2:44 AM
  • Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles Button1.Click

            PrintPreviewDialog1.ShowDialog()

     

        End Sub

     

        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _

        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

        Handles PrintDocument1.PrintPage

     

            Static counter As Integer = 1

            If counter = 1 Then

                e.Graphics.DrawString("Hello", New Font("Arial", _

                12, FontStyle.Regular), Brushes.Black, 100, 100)

                counter += 1

                e.HasMorePages = True

                Exit Sub

            End If

     

            If counter = 2 Then

                e.Graphics.DrawString("Hello Again", New Font("Arial", _

                12, FontStyle.Regular), Brushes.Black, 100, 100)

                counter = 1

                e.HasMorePages = False

            End If

     

        End Sub

    End Class

     

     

    Tuesday, August 21, 2007 3:05 AM
  • Sometimes it is much more convenient to stay in the PrintPage event and ask for a new page when needed.  This class modifies the PrintDocument class to accomplish this:

    Code Snippet

    Imports System.Threading

    Imports System.Drawing.Printing

    Public Class PrintDoc

    Inherits PrintDocument

    Private W1, W2 As EventWaitHandle

    Private PagesPrinted As Int32

    Public Event PrintPages(ByVal Sender As Object, ByRef e As System.Drawing.Printing.PrintPageEventArgs)

    Private T As Thread

    Private eRef As System.Drawing.Printing.PrintPageEventArgs

    Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    eRef = e

    If PagesPrinted = 0 Then T.Start() Else W2.Set()

    PagesPrinted += 1

    W1.WaitOne()

    End Sub

    Private Sub PrintDoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

    W1 = New EventWaitHandle(False, EventResetMode.AutoReset)

    W2 = New EventWaitHandle(False, EventResetMode.AutoReset)

    T = New Thread(AddressOf RaisePrintPages)

    PagesPrinted = 0

    End Sub

    Public Sub NextPage()

    eRef.HasMorePages = True

    W1.Set() : W2.WaitOne()

    End Sub

    Private Sub RaisePrintPages()

    RaiseEvent PrintPages(Me, eRef)

    W1.Set()

    End Sub

    Private Sub PrintDoc_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

    W1.Close() : W2.Close()

    End Sub

    End Class

     

     
    This class exposes the PrintPages event which can be used for continuous printing:
     

    Code Snippet

    Private Sub PrintDoc1_PrintPages(ByVal Sender As Object, ByRef e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc1.PrintPages

    e.Graphics.DrawString("Hello", New Font("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

    PrintDoc1.NextPage()

    e.Graphics.DrawString("Hello Again", New Font("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

    End Sub

     

     

    The call to NextPage starts a new page on the printer.

    Tuesday, August 21, 2007 7:20 AM
  • John,

     

    I'm sure your example works fine, but,

    beginners learning the ins and outs of the

    printpage event should completely absorb

    the basic (regular) way of using it before

    going on to code like yours.

     

    But thanks for the post. I'll try out your

    code when I get some time.

     

    Tuesday, August 21, 2007 11:42 PM
  • Thank you all so much for your help.  If I can't figure it out from here, I'll get some rope, duct tape and beer and have a self-hanging party. ha. Thanks again.  I will spend the night working on this and if I figure it out I will close this thread, and if not,  "I'll Be Back".

     

    Thanks again,

    Brad

    Wednesday, August 22, 2007 2:37 AM
  • hei could you explain me how do i can use the class you creted?

    can you post me a piece of code working

    I creted the class but i cant use tue metod NextPage, vb 2008 underline it to me!


    Tahnks
    Tuesday, May 12, 2009 4:43 AM
  • Sometimes it is much more convenient to stay in the PrintPage event and ask for a new page when needed.  This class modifies the PrintDocument class to accomplish this:

    Code Snippet

    Imports System.Threading

    Imports System.Drawing.Printing

    Public Class PrintDoc

    Inherits PrintDocument

    Private W1, W2 As EventWaitHandle

    Private PagesPrinted As Int32

    Public Event PrintPages(ByVal Sender As Object, ByRef e As System.Drawing.Printing.PrintPageEventArgs)

    Private T As Thread

    Private eRef As System.Drawing.Printing.PrintPageEventArgs

    Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    eRef = e

    If PagesPrinted = 0 Then T.Start() Else W2.Set()

    PagesPrinted += 1

    W1.WaitOne()

    End Sub

    Private Sub PrintDoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

    W1 = New EventWaitHandle(False, EventResetMode.AutoReset)

    W2 = New EventWaitHandle(False, EventResetMode.AutoReset)

    T = New Thread(AddressOf RaisePrintPages)

    PagesPrinted = 0

    End

    Sub

     

    Public Sub NextPage()

    eRef.HasMorePages =

    True

     

    W1.Set() : W2.WaitOne()

    End

    Sub

     

    Private Sub RaisePrintPages()

    RaiseEvent PrintPages(Me, eRef)

    W1.Set()

    End

    Sub

     

    Private Sub PrintDoc_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

    W1.Close() : W2.Close()

    End

    Sub

    End Class

     

      
    This class exposes the PrintPages event which can be used for continuous printing:
     

    Code Snippet

    Private Sub PrintDoc1_PrintPages(ByVal Sender As Object, ByRef e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc1.PrintPages

    e.Graphics.DrawString("Hello", New Font("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

    PrintDoc1.NextPage()

    e.Graphics.DrawString("Hello Again", New Font("Arial", 12, FontStyle.Regular), Brushes.Black, 100, 100)

    End Sub

     

     

    The call to NextPage starts a new page on the printer.


    Maybe a little late but… thank you very much for this code, it is the missing part in my classe.

    Wednesday, November 30, 2011 2:35 PM