打印到下一页

时间:2017-02-14 00:05:18

标签: vb.net

我已经阅读了很多关于在VB.NET中打印更多页面的不同帖子。但是,由于某种原因,我没有让它工作。一旦yPosition超过1000,我就将e.hasmorepages设置为True。然而,不是进入下一页,而是从顶部开始覆盖。我在这做错了什么?这是一个学校项目。

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

1 个答案:

答案 0 :(得分:1)

汉斯回答了这个问题:

您必须添加Exit Sub才能开始打印页面。然后事件再次触发以打印下一页。你不想再次从0开始。所以,让我成为你班上的一个领域。使用BeginPrint事件将其设置为0。

谢天谢地!