Found some code for printing documents, as I've never tried it before. My main focus is to print the content of a listbox to a sheet of paper. What I found uses a type of ListItem, which doesn't seem to be available for a Windows Form outside of web dev.

My attempt was to add all items to an array and then cycle throught the array to create a new line after every array entry. I think that portion is functioning as desired, but I'm not quite sure if I'm handling the print function correctly.

VB.NET Code:
  1. Private Sub LnkPrint_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkPrint.LinkClicked
  2.         If PrintDialog1.ShowDialog() = DialogResult.OK Then
  3.             Me.ThePrintDocument.Print()
  4.         End If
  5.     End Sub
  6.  
  7. 'Don't know if I actually need a PrintDocument tool here, but added one named ThePrintDocument.
  8.  
  9.     Protected Sub ThePrintDocument_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
  10.         Dim MyArray(Me.LstResults.Items.Count) As String
  11.         Dim linesPerPage As Single = 0
  12.         Dim yPosition As Single = 0
  13.         Dim count As Integer = 0
  14.         Dim leftMargin As Single = ev.MarginBounds.Left
  15.         Dim topMargin As Single = ev.MarginBounds.Top
  16.         Dim line As String = Nothing
  17.         Dim printFont As Font = Me.LstResults.Font
  18.         Dim myBrush As New SolidBrush(Color.Black)
  19.         Dim strText As String = ""
  20.         Dim i As Integer = 0
  21.         LstResults.Items.CopyTo(MyArray, 0)
  22.         Do Until i = Me.LstResults.Items.Count
  23.             strText = MyArray(i).ToString() + Environment.NewLine
  24.             i = i + 1
  25.         Loop
  26.         Dim myReader = New StringReader(strText)
  27.         ' Work out the number of lines per page, using the MarginBounds.
  28.         linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
  29.         ' Iterate over the string using the StringReader, printing each line.
  30.         While count < linesPerPage And Not ((line <= myReader.ReadLine() And Nothing)) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
  31.             ' calculate the next line position based on
  32.             ' 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
  33.             ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat())
  34.             count += 1
  35.         End While ' If there are more lines, print another page. if (line != null)ev.HasMorePages = True
  36.         ev.HasMorePages = False
  37.         myBrush.Dispose()
  38.     End Sub 'ThePrintDocument_PrintPage

I don't get any errors and the printer spits out a page, but it's blank.

Any help would be appreciated!