-
Jul 11th, 2011, 11:29 AM
#1
Thread Starter
Addicted Member
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:
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
I don't get any errors and the printer spits out a page, but it's blank.
Any help would be appreciated!
-
Jul 11th, 2011, 11:36 AM
#2
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
Dim ListBoxItem As String = String.Empty
For Each LBItem As String In ListBox1.Items
ListBoxItem = ListBoxItem & vbCrLf & LBItem
Next
ListBoxItem = ListBoxItem.Substring(vbCrLf.Length)
e.Graphics.DrawString(ListBoxItem, fnt, Brushes.Black, 0, 0)
e.HasMorePages = False
End Sub
-
Jul 11th, 2011, 11:40 AM
#3
Re: Printing Document from Listbox
Hack, can you please explain this line?
vb Code:
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
-
Jul 11th, 2011, 12:36 PM
#4
Thread Starter
Addicted Member
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.
-
Jul 11th, 2011, 01:14 PM
#5
Thread Starter
Addicted Member
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:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim t As Integer = -1
Dim ListBoxItem As String = String.Empty
For Each LBItem As String In LstResults.Items
If t >= 0 Then
ListBoxItem = ListBoxItem & vbCrLf & LBItem
Else
t = t + 1
End If
Next
Static Lines() As String = ListBoxItem.Split(vbCrLf)
Static Font As New Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel)
Static I As Integer
Dim VerticalPos As Integer = 20
Do
e.Graphics.DrawString(Lines(I), Font, Brushes.Black, 20, VerticalPos)
I += 1
VerticalPos += Font.Height
If VerticalPos > e.PageBounds.Bottom Then
e.HasMorePages = True
Return
End If
Loop Until I = Lines.Length
End Sub
Last edited by condonethis; Jul 11th, 2011 at 01:16 PM.
Reason: Explaining variable that might not be useful to outside coder.
-
Sep 27th, 2014, 06:59 PM
#6
Thread Starter
Addicted Member
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:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim fnt As New Font("Arial", 13, FontStyle.Regular, GraphicsUnit.Point) Dim t As Integer = 0 Dim ListBoxItem As String = String.Empty For Each LBItem As String In LstResults.Items If t >= 0 Then If ListBoxItem = String.Empty Then ListBoxItem = LBItem Else ListBoxItem = ListBoxItem & Environment.NewLine & LBItem End If End If t = t + 1 Next Static Lines() As String = ListBoxItem.Split(Environment.NewLine) Static Font As New Font("Arial", 13, FontStyle.Regular, GraphicsUnit.Pixel) Static I As Integer Dim VerticalPos As Integer = 20 e.Graphics.DrawString("Currently Unreported Items", Font, Brushes.Black, -85, -10) Dim q As Integer = 0 Do e.Graphics.DrawString(Lines(I), Font, Brushes.Black, -75, VerticalPos) I += 1 If q = 0 Then q = 1 Else VerticalPos += Font.Height End If If VerticalPos > e.PageBounds.Bottom Then e.HasMorePages = True Return End If Loop Until I = Lines.Length 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|