Printing multiple pages using PrintDocument and HasMorePages

前端 未结 1 1525
名媛妹妹
名媛妹妹 2021-01-22 07:03

I\'m trying to print a list of items in a listbox. I have 284 items. About a quarter of them get printed and the rest don\'t print and at the bottom the last entry is half way c

相关标签:
1条回答
  • 2021-01-22 07:47

    The way you wrote your code tells me you think the PrintPage method is only getting called once, and that you are using that one call to print everything. That's not the way it works.

    When a new page needs to be printed, it will call the PrintPage method again, so your loop variable has to be outside the PrintPage scope. When the next page prints, you need to know what line number you are currently printing.

    Try it like this:

    Private printLine As Integer = 0
    
    Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs)
      Dim startX As Integer = e.MarginBounds.Left
      Dim startY As Integer = e.MarginBounds.Top
      Do While printLine < SoftwareLBox.Items.Count
        If startY + SoftwareLBox.ItemHeight > e.MarginBounds.Bottom Then
          e.HasMorePages = True
          Exit Do
        End If
        e.Graphics.DrawString(SoftwareLBox.Items(printLine).ToString, SoftwareLBox.Font, _
                              Brushes.Black, startX, startY)
        startY += SoftwareLBox.ItemHeight
        printLine += 1
      Loop
    End Sub
    

    Set the printLine variable to zero before you print, or set it to zero in the BeginPrint event.

    0 讨论(0)
提交回复
热议问题