jane
jane

Reputation: 11

Print multiple pages in vb.net

How can i print multiple pages? In my form i have textboxes with their corresponding labels eg. (id, name, course, etc.) but the problem is 1 page is not enough to display all textboxes. i have to add another page to display the remaining textboxes with their labels. I tried to set e.hasmorepages to true but the textboxes appears in the second page are just the same with the first page it does not continue.

Here is my code :

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

    Dim labelFont As New Font("Arial", 11, FontStyle.Bold)
    Dim textFont As New Font("Arial", 11, FontStyle.Regular)
    Dim headerFont As New Font("Arial", 12, FontStyle.Bold)

    e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660)
    e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690)
    e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692)
    e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715)
    e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740)
    e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765)
    e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790)
    e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815)
    e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840)
    e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865)
    e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890)
    e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 1500)

    mPageNumber += 1

    e.HasMorePages = (mPageNumber <= 2)
End Sub

Upvotes: 0

Views: 10810

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

When you have more than one page, you need to ensure the single PrintPage() method is called once for every page you need to print. Each time the method is called, it needs to know which page is current and what should be written to that page.

The e.HasMorePages variable is how you make the PrintDocument object call the method again. Also remember the printSisDoc_PrintPage() method is part of a class. You can set data in the class instance the method can use to know what page is current and what to print.

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

    Dim labelFont As New Font("Arial", 11, FontStyle.Bold)
    Dim textFont As New Font("Arial", 11, FontStyle.Regular)
    Dim headerFont As New Font("Arial", 12, FontStyle.Bold)

    Select mPageNumber
     Case 1
        e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660)
        e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690)
        e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692)
        e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715)
        e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740)
        e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765)
        e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790)
        e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815)
        e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840)
        e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865)
        e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890)
        e.HasMorePages = True

     Case 2

        e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 400)
        e.HasMorePages = False

    End Select

    mPageNumber += 1

End Sub

Upvotes: 5

Related Questions