[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1481
  • Last Modified:

How Do I print a scrollable panel in VB.Net?

Hi ppl,

Have a scrollable panel in a form (panel height of about 850. Only panel is scrollable, not form). The panel is a form loaded with data which i need to print. Now we know that winforms dont have the .PrintForm function, so I tried that common code available which captures the form as a Bitmap, then sends that to the printer. Can get the panel to print, the problem is that it only prints what is visible on the screen, that is the rest of the panel which is reached via the scrollbars is not captured/printed......This is an urgent problem...any ideas?

Dont want to really deal with crystal reports instead as alot of the data is in listboxes and thus easier to print as viewed as on the screen.


1 Solution
Try using the reflection object to create bitmap images of each individual control in the panel. Then combine these images with their respective coordinates into one big panel image then send it to the printer document component. The drawback is that it does not work on all controls.

Private Function GetControlBitmap(ByVal ctl As Control) As Bitmap
            Dim Methods() As System.Reflection.MethodInfo =
ctl.GetType.GetMethods( _
                System.Reflection.BindingFlags.Instance Or
            Dim i As Integer
            Dim OnPaintMethod As System.Reflection.MethodInfo
            For i = 0 To Methods.Length - 1
                If Methods(i).Name = "OnPaint" Then
                    OnPaintMethod = Methods(i)
                    Exit For
                End If

            If Not OnPaintMethod Is Nothing Then
                Dim bmp As New Bitmap(ctl.Width, ctl.Height)
                Dim g As Graphics = Graphics.FromImage(bmp)
                Dim PaintEventArgs As New
System.Windows.Forms.PaintEventArgs(g, New Rectangle(0, 0, ctl.Width,
                OnPaintMethod.Invoke(ctl, New Object() {PaintEventArgs})
                Return bmp
            End If
        End Function

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now