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
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