Good day,

I'm having trouble when trying to convert my items in a listbox to a string and then printing the results to multiple pages if the lines of the string exceeds DefaultPageSettings.PaperSize.Height.

Please help, if you have any ideas why I continuously loop when there the document is multipaged:
vb.net Code:
  1. Sub PrintList()
  2.         Dim x As New PrintDocument
  3.         AddHandler x.PrintPage, AddressOf printDoc_PrintPage
  4.         Dim printHeight As Integer
  5.         Dim printWidth As Integer
  6.         Dim rightMargin As Integer
  7.         Dim tmp As Integer
  8.         With x.DefaultPageSettings
  9.             printHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
  10.             printWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
  11.             rightMargin = .Margins.Top   'Y
  12.         End With
  13.         'x.DefaultPageSettings.Landscape = True
  14.         If x.DefaultPageSettings.Landscape Then
  15.             tmp = printHeight
  16.             printHeight = printWidth
  17.             printWidth = tmp
  18.         End If
  19.         x.Print()
  20.     End Sub
  21.  
  22.     Private printLine As Integer = 0
  23.     Sub printDoc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
  24.         Dim textToPrint As String = Nothing
  25.         Dim printFont As New Font("Courier New", 12)
  26.         Dim leftMargin As Integer = e.MarginBounds.Left
  27.         Dim topMargin As Integer = e.MarginBounds.Top
  28.         Dim startX As Integer = e.MarginBounds.Left
  29.         Dim startY As Integer = e.MarginBounds.Top
  30.         Dim n As Integer
  31.  
  32.         For Each st As String In LstResults.Items
  33.             Dim i As Integer = 0
  34.             Dim ar As String() = st.Replace(vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab, vbTab).Replace(vbTab & vbTab & vbTab, vbTab).Split(vbTab)
  35.             For Each s As String In ar
  36.                 i = i + 1
  37.                 If i <> 4 And i <> 6 And i < 6 Then
  38.                     textToPrint &= s & "  "
  39.                 ElseIf i = 6 Then
  40.                     textToPrint &= s & nl & nl
  41.                 ElseIf i = 4 Then
  42.                     textToPrint &= nl & s & "  "
  43.                 End If
  44.             Next
  45.         Next
  46.         Dim numLines As Integer = textToPrint.Split("\n").Length
  47.         Dim myFont As New Font(LstResults.Font, 8)
  48.         Dim myFontBold As New Font("Courier New", 12, FontStyle.Regular)
  49.         Dim StringSize As New SizeF
  50.         Dim HeightSize As New SizeF
  51.  
  52.         StringSize = e.Graphics.MeasureString(textToPrint, myFont)
  53.    
  54.         Do While printLine < (numLines)
  55.             If startY + StringSize.Height > e.MarginBounds.Bottom Then
  56.                 e.HasMorePages = True
  57.                 Exit Do
  58.             End If
  59.  
  60.             Dim totalout As String() = textToPrint.Split("\n")
  61.             e.Graphics.DrawString(totalout(printLine).ToString, LstResults.Font, _
  62.                                   Brushes.Black, startX, startY)
  63.             startY += StringSize.Height
  64.             printLine += 1
  65.         Loop
  66.         'e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin)
  67.     End Sub

Thank you in advance!