Alex S.
Alex S.

Reputation: 45

Print to next page

I have read a lot of different posts on printing to more pages in VB.NET. For some reason I'm not getting it to work, however. I have e.hasmorepages set to True once the yPosition goes over 1000. Instead of going to the next page, however, it's overwriting starting from the top. What am I doing wrong here? This is a school project.

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    'Declare variables for printing position, strings, and state name
    Dim yPos As Integer = 40
    Dim xPos As Integer = 25
    Dim strLine1 As String = String.Empty
    Dim strLine2 As String = String.Empty
    Dim strLine3 As String = String.Empty
    Dim strLine4 As String = String.Empty
    Dim strLine5 As String = String.Empty

    For i = 0 To (lstRecords.Items.Count - 1)
        'Concatenate strings for printing
        strLine1 = "Record Name: " & gVinyl(i).Name
        strLine2 = "Artist: " & gVinyl(i).Artist
        strLine3 = "Released: " & gVinyl(i).Year
        strLine4 = "Contains: " & gVinyl(i).Tracks.ToString & " tracks running " & gVinyl(i).Duration & " minutes"
        strLine5 = "Size: " & gVinyl(i).Size.ToString & " inches      Speed: " & gVinyl(i).Speed.ToString & " RPM"

        'Multiple page print
        If yPos > 1000 Then
            e.HasMorePages = True
            yPos = 40
        End If

        'Position each string line for printing
        e.Graphics.DrawString(strLine1, New Font("Times New Roman", 11), Brushes.Black, xPos, yPos)
        yPos += 20
        e.Graphics.DrawString(strLine2, New Font("Times New Roman", 11), Brushes.Black, xPos, yPos)
        yPos += 20
        e.Graphics.DrawString(strLine3, New Font("Times New Roman", 11), Brushes.Black, xPos, yPos)
        yPos += 20
        e.Graphics.DrawString(strLine4, New Font("Times New Roman", 11), Brushes.Black, xPos, yPos)
        yPos += 20
        e.Graphics.DrawString(strLine5, New Font("Times New Roman", 11), Brushes.Black, xPos, yPos)
        yPos += 50
    Next

    'Last page printed
    e.HasMorePages = False
End Sub

Upvotes: 0

Views: 2829

Answers (1)

Alex S.
Alex S.

Reputation: 45

Hans answered this:

You have to add Exit Sub so it will start printing the page. Then the event fires again to print the next page. You do not want to start at 0 again. So make i a field of your class. Set it to 0 with the BeginPrint event.

THANK YOU HANS!

Upvotes: 1

Related Questions