Private Sub LnkPrint_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkPrint.LinkClicked
If PrintDialog1.ShowDialog() = DialogResult.OK Then
Me.ThePrintDocument.Print()
End If
End Sub
'Don't know if I actually need a PrintDocument tool here, but added one named ThePrintDocument.
Protected Sub ThePrintDocument_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim MyArray(Me.LstResults.Items.Count) As String
Dim linesPerPage As Single = 0
Dim yPosition As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
Dim printFont As Font = Me.LstResults.Font
Dim myBrush As New SolidBrush(Color.Black)
Dim strText As String = ""
Dim i As Integer = 0
LstResults.Items.CopyTo(MyArray, 0)
Do Until i = Me.LstResults.Items.Count
strText = MyArray(i).ToString() + Environment.NewLine
i = i + 1
Loop
Dim myReader = New StringReader(strText)
' Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Iterate over the string using the StringReader, printing each line.
While count < linesPerPage And Not ((line <= myReader.ReadLine() And Nothing)) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
' calculate the next line position based on
' the height of the font according to the printing device yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));// draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat())
count += 1
End While ' If there are more lines, print another page. if (line != null)ev.HasMorePages = True
ev.HasMorePages = False
myBrush.Dispose()
End Sub 'ThePrintDocument_PrintPage