Results 1 to 10 of 10

Thread: Print a listview to a printer

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Print a listview to a printer

    I have a listview with about 200 lines that I am trying to print as a report to a printer. I thought this would be something I could find examples of easily, but all I can seem to find are for vb6 and earlier. I am using vb.net 2010. Either this is so easy to do that nobody has asked before or I am very bad at searching. I have to believe that printing a listview is common task. A working example would be great.

    Thanks,
    Jim

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print a listview to a printer

    Quote Originally Posted by JimAvanti View Post
    Either this is so easy to do that nobody has asked before or I am very bad at searching.
    The latter I'm afraid. Printing in Windows Forms is basically the same regardless of what you're printing: you use a PrintDocument, call its Print method and handle its PrintPage event, using GDI+ to draw whatever you want printed. There's nothing special about a ListView in this regard, other than what specific GDI+ code you write to print the data in the way you want it displayed.

    I would suggest that you first visit the VB.NET CodeBank forum, sort by Thread Starter and then find the printing tutorial posted by Merrion. Once you know the generalities of printing, you can decide exactly how you want the data displayed and determine how to do that using GDI+ and add the appropriate code to the PrintPage event handler.

  3. #3
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: Print a listview to a printer

    Practice Practice.

    This example just loops the list and prints each item on a new line. There are many ways to do it.

    To make the example just cut and paste the code into an empty form.

    The example shows the print preview dialog with multiple pages. Click the print button on the dialog to print to the printer.

    Here is another example.

    Name:  PrintListView.png
Views: 3796
Size:  17.7 KB

    Code:
    Imports System.Drawing.Printing
    
    Public Class PrintListView
        Private WithEvents Listbox1 As New ListBox With {.Parent = Me,
            .Location = New Point(20, 20), .Size = New Size(200, 200)}
        Private WithEvents PrintDocument1 As New PrintDocument
        Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
        Private MasterData, PrintString As String
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Print Listview"
            For i As Integer = 1 To 200
                Listbox1.Items.Add("Item Number " & i.ToString & " of Listview Data")
            Next
    
            For Each i As String In Listbox1.Items
                MasterData += i & vbLf
            Next
    
            PrintString = MasterData
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            Dim charactersOnPage, linesPerPage As Integer
    
            Using f As Font = New Font("Consolas", 12)
                ' Sets the value of charactersOnPage to the number of characters  
                ' of stringToPrint that will fit within the bounds of the page.
                e.Graphics.MeasureString(PrintString, f, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
                ' Draws the string within the bounds of the page
                e.Graphics.DrawString(PrintString, f, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)
    
            End Using
    
            ' Remove the portion of the string that has been printed.
            PrintString = PrintString.Substring(charactersOnPage)
    
            ' Check to see if more pages are to be printed.
            e.HasMorePages = PrintString.Length > 0
    
            'reload string for actual printing after preview
            If Not e.HasMorePages Then PrintString = MasterData
        End Sub
    End Class

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Re: Print a listview to a printer

    Tommy,

    I used your example in my program and looped through my ListView to build the string and it worked (Which is great for my learning a bit about building a list to print), however what I need to do is much more. I need to print the Listview as it is on the screen (Formatted with the cells and also color highlighted background and text). I was wondering if there was code already written out there that would do this. I'll keep looking.

    Thanks,

    Jim



    Quote Originally Posted by tommytwotrain View Post
    Practice Practice.

    This example just loops the list and prints each item on a new line. There are many ways to do it.

    To make the example just cut and paste the code into an empty form.

    The example shows the print preview dialog with multiple pages. Click the print button on the dialog to print to the printer.

    Here is another example.

    Name:  PrintListView.png
Views: 3796
Size:  17.7 KB

    Code:
    Imports System.Drawing.Printing
    
    Public Class PrintListView
        Private WithEvents Listbox1 As New ListBox With {.Parent = Me,
            .Location = New Point(20, 20), .Size = New Size(200, 200)}
        Private WithEvents PrintDocument1 As New PrintDocument
        Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
        Private MasterData, PrintString As String
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Print Listview"
            For i As Integer = 1 To 200
                Listbox1.Items.Add("Item Number " & i.ToString & " of Listview Data")
            Next
    
            For Each i As String In Listbox1.Items
                MasterData += i & vbLf
            Next
    
            PrintString = MasterData
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            Dim charactersOnPage, linesPerPage As Integer
    
            Using f As Font = New Font("Consolas", 12)
                ' Sets the value of charactersOnPage to the number of characters  
                ' of stringToPrint that will fit within the bounds of the page.
                e.Graphics.MeasureString(PrintString, f, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
                ' Draws the string within the bounds of the page
                e.Graphics.DrawString(PrintString, f, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)
    
            End Using
    
            ' Remove the portion of the string that has been printed.
            PrintString = PrintString.Substring(charactersOnPage)
    
            ' Check to see if more pages are to be printed.
            e.HasMorePages = PrintString.Length > 0
    
            'reload string for actual printing after preview
            If Not e.HasMorePages Then PrintString = MasterData
        End Sub
    End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Print a listview to a printer

    This might help. More functionality than a plain text list printing...

    https://code.msdn.microsoft.com/List...inter-7a9be0a7

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Re: Print a listview to a printer

    Paul,

    I tried the example you sent me and I removed all reference to groups and shortened the list and columns and everything works is the downloaded project, however I am getting errors I don’t understand when I try to use it with my program. Have you used this example in any programs?

    The line:
    maxPagesWide = pages.Keys.Max + 1
    gives the error:
    'Max' is not a member of 'System.Collections.Generic.Dictionary(Of Integer, Equipment_Tracker.listViewPrinter.pageDetails).KeyCollection'.

    The lines:
    items = lv.Items.Cast(Of ListViewItem).ToArray

    Dim r2 As New Rectangle(location, New Size(lv.Columns.Cast(Of ColumnHeader).Skip(pages(0).startCol).Take(pages(0).columns).Sum(Function(ch As ColumnHeader) ch.Width), titleHeight))

    Both give the error:
    'Cast' is not a member of 'System.Windows.Forms.ListView.ListViewItemCollection'.

    Any ideas?

    Thanks,
    Jim



    Quote Originally Posted by .paul. View Post
    This might help. More functionality than a plain text list printing...

    https://code.msdn.microsoft.com/List...inter-7a9be0a7

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Print a listview to a printer

    @Jim...

    Those are all linq functions that are causing errors there. Which version of the framework are you targetting? Could you post a screenshot of your Project-->Properties-->References page?

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Re: Print a listview to a printer

    That was the problem. The downloaded project had system.linq checked and the program I was working on did not. I am not familiar with that ref. I guess I need to read up on it. Both target Framwork 4.0

    I am now working on modifying the printed page to print landscape and i am noticing the maxHeight needs to reference the width of the page and the MaxWidth needs to reference the height of the page when you set the page landscape to true. I'm sure there is a lot more than that to change, but it is a start and it is working without errors now. I will post the modified ListViewprint class...

    Thanks,
    Jim


    Quote Originally Posted by .paul. View Post
    @Jim...

    Those are all linq functions that are causing errors there. Which version of the framework are you targetting? Could you post a screenshot of your Project-->Properties-->References page?

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Re: Print a listview to a printer

    Paul,
    You were correct on the missing ref. It is working now. This is the modified version of the PrintListView you sent me the link to: I removed the group option and started to work on printing landscape.

    Code:
    Public Class listViewPrinter
    
        Private lv As ListView
        Private location As Point
        Private border As Boolean
        Private title As String
        Private titleHeight As Integer
        Private hLines As Boolean
        Private vLines As Boolean
    
        Private WithEvents pd As New Printing.PrintDocument
    
        Public Sub New(ByVal lv As ListView, ByVal location As Point, ByVal border As Boolean, ByVal _hLines As Boolean, ByVal _vLines As Boolean, Optional ByVal title As String = "")
            Me.lv = lv
            Me.location = location
            Me.border = border
            Me.title = title
            Me.hLines = _hLines
            Me.vLines = _vLines
            titleHeight = If(title <> "", lv.FindForm.CreateGraphics.MeasureString(title, New Font(lv.Font.Name, 25)).ToSize.Height, 0)
        End Sub
    
        Public Sub print()
            'pd.Print()
            Dim ppd As New PrintPreviewDialog
            ppd.Document = pd
            ppd.WindowState = FormWindowState.Maximized
            ppd.ShowDialog()
        End Sub
    
        Private Structure pageDetails       ' structure to hold printed page details
            Dim columns As Integer
            Dim rows As Integer
            Dim startCol As Integer
            Dim startRow As Integer
            Dim headerIndices As List(Of Integer)
        End Structure
    
        Private pages As Dictionary(Of Integer, pageDetails)        ' dictionary to hold printed page details, with index key
        Dim maxPagesWide As Integer
        Dim maxPagesTall As Integer
        Dim items() As ListViewItem
    
        ' the majority of this Sub is calculating printed page ranges
        Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
            pd.DefaultPageSettings.Landscape = True
            pd.OriginAtMargins = True   'this removes the printed page margins
            pd.DefaultPageSettings.Margins = New Drawing.Printing.Margins(location.X, location.X, location.Y, location.Y)
            pages = New Dictionary(Of Integer, pageDetails)
            ' Reversed MaxWidth & MaxHeight for landscape page...
            'Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 40)
            'Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 40)
            Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 0)
            Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 0)
    
            Dim pageCounter As Integer = 0
            pages.Add(pageCounter, New pageDetails With {.headerIndices = New List(Of Integer)})
            Dim columnCounter As Integer = 0
            Dim columnSum As Integer = 0
    
            For c As Integer = 0 To lv.Columns.Count - 1
                If columnSum + lv.Columns(c).Width < maxWidth Then
                    columnSum += lv.Columns(c).Width
                    columnCounter += 1
                Else
                    pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
                    columnSum = lv.Columns(c).Width
                    columnCounter = 1
                    pageCounter += 1
                    pages.Add(pageCounter, New pageDetails With {.startCol = c, .headerIndices = New List(Of Integer)})
                End If
                If c = lv.Columns.Count - 1 Then
                    If pages(pageCounter).columns = 0 Then
                        pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
                    End If
                End If
            Next
            maxPagesWide = pages.Keys.Max + 1
            pageCounter = 0
            Dim rowCounter As Integer = 0
            Dim counter As Integer = 0
            Dim itemHeight As Integer = lv.GetItemRect(0).Height
            Dim rowSum As Integer = itemHeight
    
            For r As Integer = 0 To lv.Items.Count - 1
                counter += 1
                If rowSum + itemHeight < maxHeight Then
                    rowSum += itemHeight
                    rowCounter += 1
                Else
                    pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
                    For x As Integer = 1 To maxPagesWide - 1
                        pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
                    Next
                    pageCounter += maxPagesWide
                    For x As Integer = 0 To maxPagesWide - 1
                        pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = counter - 1})
                    Next
                    rowSum = itemHeight * 2
                    rowCounter = 1
                End If
                If counter = lv.Items.Count Then
                    For x As Integer = 0 To maxPagesWide - 1
                        If pages(pageCounter + x).rows = 0 Then
                            pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
                        End If
                    Next
                End If
            Next
            maxPagesTall = pages.Count \ maxPagesWide
            items = lv.Items.Cast(Of ListViewItem).ToArray
        End Sub
    
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
    
            Dim r2 As New Rectangle(location, New Size(lv.Columns.Cast(Of ColumnHeader).Skip(pages(0).startCol).Take(pages(0).columns).Sum(Function(ch As ColumnHeader) ch.Width), titleHeight))
            e.Graphics.DrawString(title, New Font(lv.Font.Name, 25), Brushes.Black, r2, sf)
            sf.Alignment = StringAlignment.Near
            Dim startX As Integer = location.X
            Dim startY As Integer = location.Y + titleHeight + 12
            Static startPage As Integer = 0
            Dim itemHeight As Integer = lv.GetItemRect(0).Height
            Dim bottomRight As Point
    
            For p As Integer = startPage To pages.Count - 1
                startX = location.X
                startY = location.Y + titleHeight + 12
                Dim cell As Rectangle
                For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                    cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
                    e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
                    e.Graphics.DrawRectangle(Pens.Black, cell)
                    e.Graphics.DrawString(lv.Columns(c).Text, lv.Font, Brushes.Black, cell, sf)
                    startX += lv.Columns(c).Width
                Next
                startY += itemHeight
                startX = location.X
                For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
                    startX = location.X
                    For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                        cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
                        e.Graphics.DrawString(items(r).SubItems(c).Text, lv.Font, Brushes.Black, cell, sf)
                        If vLines Then
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Left, cell.Bottom)
                            e.Graphics.DrawLine(Pens.Black, cell.Right, cell.Top, cell.Right, cell.Bottom)
                        End If
                        If hLines Then
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Right, cell.Top)
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Bottom, cell.Right, cell.Bottom)
                        End If
                        startX += lv.Columns(c).Width
                    Next
                    startY += itemHeight
                    If r = pages(p).startRow + pages(p).rows - 1 Then
                        bottomRight = New Point(startX, startY)
                        If border Then e.Graphics.DrawRectangle(Pens.Black, New Rectangle(location, New Size(bottomRight.X - location.X, bottomRight.Y - location.Y)))
                    End If
                Next
                If p <> pages.Count - 1 Then
                    startPage = p + 1
                    e.HasMorePages = True
                    Return
                Else
                    startPage = 0
                End If
            Next
        End Sub
    
    End Class

    Quote Originally Posted by .paul. View Post
    @Jim...

    Those are all linq functions that are causing errors there. Which version of the framework are you targetting? Could you post a screenshot of your Project-->Properties-->References page?

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2014
    Posts
    55

    Re: Print a listview to a printer

    Paul,
    You were correct on the missing ref. It is working now. This is the modified version of the PrintListView you sent me the link to: I removed the group option and started to work on printing landscape.

    Code:
    Public Class listViewPrinter
    
        Private lv As ListView
        Private location As Point
        Private border As Boolean
        Private title As String
        Private titleHeight As Integer
        Private hLines As Boolean
        Private vLines As Boolean
    
        Private WithEvents pd As New Printing.PrintDocument
    
        Public Sub New(ByVal lv As ListView, ByVal location As Point, ByVal border As Boolean, ByVal _hLines As Boolean, ByVal _vLines As Boolean, Optional ByVal title As String = "")
            Me.lv = lv
            Me.location = location
            Me.border = border
            Me.title = title
            Me.hLines = _hLines
            Me.vLines = _vLines
            titleHeight = If(title <> "", lv.FindForm.CreateGraphics.MeasureString(title, New Font(lv.Font.Name, 25)).ToSize.Height, 0)
        End Sub
    
        Public Sub print()
            'pd.Print()
            Dim ppd As New PrintPreviewDialog
            ppd.Document = pd
            ppd.WindowState = FormWindowState.Maximized
            ppd.ShowDialog()
        End Sub
    
        Private Structure pageDetails       ' structure to hold printed page details
            Dim columns As Integer
            Dim rows As Integer
            Dim startCol As Integer
            Dim startRow As Integer
            Dim headerIndices As List(Of Integer)
        End Structure
    
        Private pages As Dictionary(Of Integer, pageDetails)        ' dictionary to hold printed page details, with index key
        Dim maxPagesWide As Integer
        Dim maxPagesTall As Integer
        Dim items() As ListViewItem
    
        ' the majority of this Sub is calculating printed page ranges
        Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
            pd.DefaultPageSettings.Landscape = True
            pd.OriginAtMargins = True   'this removes the printed page margins
            pd.DefaultPageSettings.Margins = New Drawing.Printing.Margins(location.X, location.X, location.Y, location.Y)
            pages = New Dictionary(Of Integer, pageDetails)
            ' Reversed MaxWidth & MaxHeight for landscape page...
            'Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 40)
            'Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 40)
            Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 0)
            Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 0)
    
            Dim pageCounter As Integer = 0
            pages.Add(pageCounter, New pageDetails With {.headerIndices = New List(Of Integer)})
            Dim columnCounter As Integer = 0
            Dim columnSum As Integer = 0
    
            For c As Integer = 0 To lv.Columns.Count - 1
                If columnSum + lv.Columns(c).Width < maxWidth Then
                    columnSum += lv.Columns(c).Width
                    columnCounter += 1
                Else
                    pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
                    columnSum = lv.Columns(c).Width
                    columnCounter = 1
                    pageCounter += 1
                    pages.Add(pageCounter, New pageDetails With {.startCol = c, .headerIndices = New List(Of Integer)})
                End If
                If c = lv.Columns.Count - 1 Then
                    If pages(pageCounter).columns = 0 Then
                        pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
                    End If
                End If
            Next
            maxPagesWide = pages.Keys.Max + 1
            pageCounter = 0
            Dim rowCounter As Integer = 0
            Dim counter As Integer = 0
            Dim itemHeight As Integer = lv.GetItemRect(0).Height
            Dim rowSum As Integer = itemHeight
    
            For r As Integer = 0 To lv.Items.Count - 1
                counter += 1
                If rowSum + itemHeight < maxHeight Then
                    rowSum += itemHeight
                    rowCounter += 1
                Else
                    pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
                    For x As Integer = 1 To maxPagesWide - 1
                        pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
                    Next
                    pageCounter += maxPagesWide
                    For x As Integer = 0 To maxPagesWide - 1
                        pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = counter - 1})
                    Next
                    rowSum = itemHeight * 2
                    rowCounter = 1
                End If
                If counter = lv.Items.Count Then
                    For x As Integer = 0 To maxPagesWide - 1
                        If pages(pageCounter + x).rows = 0 Then
                            pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
                        End If
                    Next
                End If
            Next
            maxPagesTall = pages.Count \ maxPagesWide
            items = lv.Items.Cast(Of ListViewItem).ToArray
        End Sub
    
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
    
            Dim r2 As New Rectangle(location, New Size(lv.Columns.Cast(Of ColumnHeader).Skip(pages(0).startCol).Take(pages(0).columns).Sum(Function(ch As ColumnHeader) ch.Width), titleHeight))
            e.Graphics.DrawString(title, New Font(lv.Font.Name, 25), Brushes.Black, r2, sf)
            sf.Alignment = StringAlignment.Near
            Dim startX As Integer = location.X
            Dim startY As Integer = location.Y + titleHeight + 12
            Static startPage As Integer = 0
            Dim itemHeight As Integer = lv.GetItemRect(0).Height
            Dim bottomRight As Point
    
            For p As Integer = startPage To pages.Count - 1
                startX = location.X
                startY = location.Y + titleHeight + 12
                Dim cell As Rectangle
                For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                    cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
                    e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
                    e.Graphics.DrawRectangle(Pens.Black, cell)
                    e.Graphics.DrawString(lv.Columns(c).Text, lv.Font, Brushes.Black, cell, sf)
                    startX += lv.Columns(c).Width
                Next
                startY += itemHeight
                startX = location.X
                For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
                    startX = location.X
                    For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                        cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
                        e.Graphics.DrawString(items(r).SubItems(c).Text, lv.Font, Brushes.Black, cell, sf)
                        If vLines Then
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Left, cell.Bottom)
                            e.Graphics.DrawLine(Pens.Black, cell.Right, cell.Top, cell.Right, cell.Bottom)
                        End If
                        If hLines Then
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Right, cell.Top)
                            e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Bottom, cell.Right, cell.Bottom)
                        End If
                        startX += lv.Columns(c).Width
                    Next
                    startY += itemHeight
                    If r = pages(p).startRow + pages(p).rows - 1 Then
                        bottomRight = New Point(startX, startY)
                        If border Then e.Graphics.DrawRectangle(Pens.Black, New Rectangle(location, New Size(bottomRight.X - location.X, bottomRight.Y - location.Y)))
                    End If
                Next
                If p <> pages.Count - 1 Then
                    startPage = p + 1
                    e.HasMorePages = True
                    Return
                Else
                    startPage = 0
                End If
            Next
        End Sub
    
    End Class

    Quote Originally Posted by .paul. View Post
    @Jim...

    Those are all linq functions that are causing errors there. Which version of the framework are you targetting? Could you post a screenshot of your Project-->Properties-->References page?

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