|
-
Apr 13th, 2004, 02:03 PM
#1
Thread Starter
Member
Printing a Listview
Has anybody figured out a good way of printing the contents of a listview control?
-
Apr 13th, 2004, 06:50 PM
#2
Hyperactive Member
I have done this before. The code below is from memory but it should be close enough to get you what you want.
Code:
Dim objPrint As New PrintDoc
FileOpen(1, "TEMPFILE.TXT", OpenMode.Output)
for n = 0 to (myListView.items.count - 1)
WriteLine(1, myListview.items(n))
next
FileClose(1)
objPrint.PrintFile("TEMPFILE.TXT")
-
Apr 15th, 2004, 06:04 PM
#3
Thread Starter
Member
PrintDoc is not a valid object. PrintDocument is, but PrintFile is not a valid method of the PrintDocument object.
-
Apr 15th, 2004, 07:51 PM
#4
Hyperactive Member
Sorry about that. I went back and looked at my own code and realized I was referencing a custom class I forgot about. Create a new class and paste the following code into it....
Once you add that you will then have the class you need for the code.
Code:
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Public Class PrintDoc
Private printFont As Font
Private streamToPrint As StreamReader
'Event fired when the user presses the print button
Public Sub PrintFile(ByVal strFileToPrint As String)
Try
streamToPrint = New StreamReader(strFileToPrint)
Try
printFont = New Font("Lucida Console", 9)
Dim pd As PrintDocument = New PrintDocument() 'Assumes the default printer
AddHandler pd.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.pd_PrintPage)
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("An error occurred printing the file - " + ex.Message)
End Try
End Sub
'Event fired for each page to print
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = 30
Dim topMargin As Single = 60
Dim line As String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
'Now iterate over the file printing out each line
'NOTE WELL: This assumes that a single line is not wider than the page width
'Check count first so that we don't read line that we won't print
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'If we have more lines then print another page
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
-
Apr 15th, 2004, 11:39 PM
#5
Thread Starter
Member
Thanks a lot. I've been struggling over this problem for months!
-
May 19th, 2006, 04:02 AM
#6
Hyperactive Member
Re: Printing a Listview
Nice Code!
Somebody help me and explain to me how i can use this. to be shown in a print preview!
When i use the above method i get an exception on the writeline saying that procedure or argument not valid. I declared n as an integer.
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
|