0

hello folks trying to print a scrollable panel containing labels, text boxes and picture boxes and list views which is probably more than two pages. I have tried and search for help online, i can get labels and text boxes printed, how do i make list view to be printed.

Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0

If page = 1 Then
    For Each ctrl As Control In Me.Panel1.Controls
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
            ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
            If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
        End If
    Next
End If

For Each ctrl As Control In Me.Panel1.Controls
    If CInt(ctrl.Tag) = page Then
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
            Dim sf As New System.Drawing.StringFormat
            If TypeOf ctrl Is TextBox Then
                If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            ElseIf TypeOf ctrl Is Label Then
                If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                    sf.Alignment = StringAlignment.Near
                ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                    sf.Alignment = StringAlignment.Far
                End If
            End If
            sf.FormatFlags = StringFormatFlags.NoClip
            e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
        ElseIf TypeOf ctrl Is PictureBox Then
            If Not DirectCast(ctrl, PictureBox).image  is nothing Then
                    e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
                End If
        End If
    End If
Next

page += 1
If page > maxPages Then
    e.HasMorePages = False
    page = 1
    maxPages = 0
Else
    e.HasMorePages = True
End If
IT PHARMA
  • 13
  • 6
  • So have you debugged to determine exactly where the exception is thrown and what argument is null? My guess would be that the `Image` property is not set when here: `e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))`. You don't have guess though, because Microsoft provided you with a debugger. – jmcilhinney Aug 23 '19 at 02:47
  • Obviously you can't draw an `Image` that doesn't exist. The obvious solution should be to test whether it exists first. – jmcilhinney Aug 23 '19 at 10:08
  • The answer is all through your code already, if you'll just think logically. The issue for many people much of the time is that they want to think in code rather than think in logic and then implement in code. If you can already check whether a control is a particular type and whether a `Tag` is equal to a particular page number, how can it be that you can check whether an `Image` is `Nothing`? – jmcilhinney Aug 23 '19 at 15:22
  • Don't post your code in a comment. Update your question with the actual code. – jmcilhinney Aug 24 '19 at 13:34

1 Answers1

0

Since the listview on the panel is not scrollable i was advice to capture it as image by adding some few code to the existing one.

 Static page As Integer = 1
    Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    Static maxPages As Integer = 0

    If page = 1 Then
        For Each ctrl As Control In Me.Panel1.Controls
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl is ListView Then
                ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
                If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
            End If
        Next
    End If

    For Each ctrl As Control In Me.Panel1.Controls
        If CInt(ctrl.Tag) = page Then
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
                Dim sf As New System.Drawing.StringFormat
                If TypeOf ctrl Is TextBox Then
                    If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                        sf.Alignment = StringAlignment.Far
                    Else
                        sf.Alignment = StringAlignment.Near
                    End If
                ElseIf TypeOf ctrl Is Label Then
                    If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                        sf.Alignment = StringAlignment.Near
                    ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                        sf.Alignment = StringAlignment.Far
                    End If
                End If
                sf.FormatFlags = StringFormatFlags.NoClip
                e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)

            ElseIf TypeOf ctrl Is ListView Then
                Dim lv As ListView = DirectCast(ctrl, ListView)
                Dim img As New Bitmap(lv.Width, lv.Height)
                lv.DrawToBitmap(img, New Rectangle(Point.Empty, img.Size))
                e.Graphics.DrawImage(img, New PointF(ctrl.Left, ctrl.Top - startPosition))
            End If
        End If
    Next

    page += 1
    If page > maxPages Then
        e.HasMorePages = False
        page = 1
        maxPages = 0
    Else
        e.HasMorePages = True
    End If
IT PHARMA
  • 13
  • 6