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

Posted on 2004-11-28
Last Modified: 2010-07-27
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.


Question by:aaronman
    1 Comment
    LVL 1

    Accepted 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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    A short article about a problem I had getting the GPS LocationListener working.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now