Vb.net Print windows form with scroll bar


I have laid out labels on a form to simulate a report.  I want to be able to print the form so that I do not have to re create the report using a report designer.   I have found code that will allow me to print what is viewable however this report is somewhat long and requires a scroll bar.  

How do I print the form so that all the information is printed?   I would prefer VB.net code using VS 2005.  However I can translate C# or switch to VS 2008.

Thanks in advance!
Who is Participating?
SZachmannAuthor Commented:
Here is the code I used to print the form.  I put everything I wanted to print into a group box called 'gbprintReport' this was because my form had a tab control with a scroll bar so the form size was not the correct height and width.
    Dim WithEvents PD As New Printing.PrintDocument
    Dim PPD As New PrintPreviewDialog
  Private Sub btnPrintForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintForm.Click
        PPD.Document = PD
    End Sub
    Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
        Dim Bmp As New Bitmap(gbprintReport.Width, gbprintReport.Height)
        gbprintReport.BackColor = Color.White
        gbprintReport.DrawToBitmap(Bmp, New Rectangle(0, 0, Bmp.Width, Bmp.Height))
        e.Graphics.DrawImage(Bmp, 0, 0)
        gbprintReport.BackColor = Color.Transparent
    End Sub

Open in new window


The javascript function Window.print() should work for what you're asking.

I imagine you have a button to print the page, this button should call the print function:

<asp:Button id="btnPrint" runat="server" text="Print" onclick="window.print()" />

"onclick" can also be replaced by "OnClientClick" (which is also available on VS2005 if I remember correctly).

I hope this works for you.
I'm sorry, I thought I was in the ASP.Net section.
My solution won't work for Windows Forms of course.

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

SZachmannAuthor Commented:
TharkunX thanks for posting but yes this is a windows form.  
You can set the
and then resize the form to show all the contents. I am not sure about the class you are using but if this class takes into account the full form rather than the screen size, you can resize the form beyond the screen limits, print it, and then restore it to previous size.
SZachmannAuthor Commented:

I was able to figure it out and I did not need to utilize you mentioned.  However thank you for the reply.  I will post the code I used to print the form a bit later.

Thanks EE
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.