Results 1 to 6 of 6

Thread: Printing a Listview

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Miami FL
    Posts
    38

    Printing a Listview

    Has anybody figured out a good way of printing the contents of a listview control?

  2. #2
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    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")
    SCUZ

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Miami FL
    Posts
    38
    PrintDoc is not a valid object. PrintDocument is, but PrintFile is not a valid method of the PrintDocument object.

  4. #4
    Hyperactive Member scuzymoto's Avatar
    Join Date
    Aug 1999
    Location
    Washington State
    Posts
    316
    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
    SCUZ

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Miami FL
    Posts
    38
    Thanks a lot. I've been struggling over this problem for months!

  6. #6
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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
  •  



Click Here to Expand Forum to Full Width