Results 1 to 6 of 6

Thread: Printing Document from Listbox

  1. #1

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Question Printing Document from Listbox

    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!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Printing Document from Listbox

    This is what I use to print the contents of a listbox and it seems to work well
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         PrintDocument1.Print()
    3. End Sub
    4.  
    5. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    6.         Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
    7.         Dim ListBoxItem As String = String.Empty
    8.         For Each LBItem As String In ListBox1.Items
    9.             ListBoxItem = ListBoxItem & vbCrLf & LBItem
    10.         Next
    11.         ListBoxItem = ListBoxItem.Substring(vbCrLf.Length)
    12.         e.Graphics.DrawString(ListBoxItem, fnt, Brushes.Black, 0, 0)
    13.         e.HasMorePages = False
    14. End Sub

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Printing Document from Listbox

    Hack, can you please explain this line?

    vb Code:
    1. ListBoxItem = ListBoxItem.Substring(vbCrLf.Length)

    never mind, it looks like you are only leaving out the first CfLf you put on the string.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  4. #4

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Printing Document from Listbox

    This is a great start, but if my text extends further than one page in length then the additional text is not carried to any additional pages.

  5. #5

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Printing Document from Listbox

    Found a solution to the > 1 page issue. I'm using the t variable to remove the first line that was serving as the header, but can't get the VbTab to line up the same.

    Thanks for your help Hack!

    vb Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim t As Integer = -1
    3.         Dim ListBoxItem As String = String.Empty
    4.         For Each LBItem As String In LstResults.Items
    5.             If t >= 0 Then
    6.                 ListBoxItem = ListBoxItem & vbCrLf & LBItem
    7.             Else
    8.                 t = t + 1
    9.             End If
    10.  
    11.         Next
    12.         Static Lines() As String = ListBoxItem.Split(vbCrLf)
    13.         Static Font As New Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel)
    14.         Static I As Integer
    15.         Dim VerticalPos As Integer = 20
    16.         Do
    17.             e.Graphics.DrawString(Lines(I), Font, Brushes.Black, 20, VerticalPos)
    18.             I += 1
    19.             VerticalPos += Font.Height
    20.             If VerticalPos > e.PageBounds.Bottom Then
    21.                 e.HasMorePages = True
    22.                 Return
    23.             End If
    24.         Loop Until I = Lines.Length
    25.     End Sub
    Last edited by condonethis; Jul 11th, 2011 at 01:16 PM. Reason: Explaining variable that might not be useful to outside coder.

  6. #6

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Printing Document from Listbox

    I had some issues with the positioning of the document once the listbox had been drawn to print. This code here seemed to fix all of my issues with vertical positioning and skipping the initial line of the listbox!
    vb.net Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim fnt As New Font("Arial", 13, FontStyle.Regular, GraphicsUnit.Point)
    3.         Dim t As Integer = 0
    4.         Dim ListBoxItem As String = String.Empty
    5.         For Each LBItem As String In LstResults.Items
    6.             If t >= 0 Then
    7.                 If ListBoxItem = String.Empty Then
    8.                     ListBoxItem = LBItem
    9.                 Else
    10.                     ListBoxItem = ListBoxItem & Environment.NewLine & LBItem
    11.                 End If
    12.             End If
    13.             t = t + 1
    14.         Next
    15.         Static Lines() As String = ListBoxItem.Split(Environment.NewLine)
    16.         Static Font As New Font("Arial", 13, FontStyle.Regular, GraphicsUnit.Pixel)
    17.         Static I As Integer
    18.         Dim VerticalPos As Integer = 20
    19.         e.Graphics.DrawString("Currently Unreported Items", Font, Brushes.Black, -85, -10)
    20.         Dim q As Integer = 0
    21.         Do
    22.             e.Graphics.DrawString(Lines(I), Font, Brushes.Black, -75, VerticalPos)
    23.             I += 1
    24.             If q = 0 Then
    25.                 q = 1
    26.             Else
    27.                 VerticalPos += Font.Height
    28.             End If
    29.             If VerticalPos > e.PageBounds.Bottom Then
    30.                 e.HasMorePages = True
    31.                 Return
    32.             End If
    33.         Loop Until I = Lines.Length
    34.     End Sub

    Hope this helps anyone else having an issue with printing a listbox!
    Last edited by condonethis; Sep 27th, 2014 at 07:07 PM. Reason: Had an extra Environment.Newline

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width