XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

SepOctober 2008Nov
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Archives

Topics

Ramblings

VB.NET

a.k.a.  The Power of the PrintForm Power Pack!

Introduction
Let's face it, ease of printing in VB.NET has never been its main selling point. In fact, because you literally have to draw most of what you want transferred to paper (or save some kind of screen capture to an image file and then print that), it really can be a bit of a pain.

I knew that the Microsoft VB Team had created an additional Power Pack which prints Windows Forms directly to the printed page but hadn't got round to trying it until today. I have to say that I'm impressed with its ease of use, the comprehensive help that comes with it and, in fact, the range of options available.

Downloading and Installing
The Pack is available as a free download from here.  Once you download, run and install the pack you are then able to add the PrintForm component to your ToolBox. I used the VB 2005 Express Edition to trial this, as the Pro and higher editions come with Crystal Reports bundled. That said, this component is so easy to use that you may well prefer it over CR for anything but the most complex displays.

If all goes well, you will automatically be presented with a comprehensive HTML based help page. If you don't see it, or you want to refer to it again later, it is saved by default to C:\Program Files\Microsoft Visual Basic 2005 Power Packs\Printing\PrintForm Component 1.0\Documentation\PrintFormHelp.htm
There is also a demo solution included in that PrintForm Component 1.0 folder.

Adding the Component to the Toolbox
Once installed on your system, adding the item to your Toolbox follows standard procedure.

   Right Click your mouse in the Toolbox.
   Select "Choose Items" from the context menu that appears.
   In the Choose Toolbox Items dialog box, navigate to the .NET Framework Components list.
   Select the PrintForm item from this list and check its checkbox.
   Click OK to add it to the Toolbox.

Trying It Out
Before I spotted that demo solution, I'd given it a run out myself to see if it worked as advertised. The form I created had some standard controls - PictureBox, RichTextBox, Label, TextBox and Button.
I also used GDI+ to draw some circles on the form using the OnPaint event. I thought it might be useful to see how the component would handle those :

Windows Form

As you can see, the RichTextBox content contains coloured text as well as a block of text that is highlighted.

I wanted the form to be printed WYSIWYG - well, almost. I decided that it would be better if the button wasn't printed to paper.

So that was my requirement: print everything on the form except the button.
How easy was it? Simple!
How successful was it? Totally!

Step By Step
Obviously, the first thing to do is to drag an instance of the PrintForm component on to your form. (Although I say "obviously", in reality you don't need to do so; you can always programmatically create an instance in your code if you wish). Anyhow, assuming you've gone the Toolbox instance route you will see that a PrintForm component named PrintForm1 now sits in the component tray at the base of your Design Window. You can now code against this component using the various methods and properties available.

First though, here's the code I used to populate the RichTextBox:

Code Copy
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RichTextBox1.LoadFile("C:\readme.rtf", RichTextBoxStreamType.RichText)
    End Sub

And the following code in the OnPaint event draws those two circles round the PictureBox:

Code Copy
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Dim p As Pen = New Pen(Color.Coral, 3)
        e.Graphics.DrawEllipse(p, New Rectangle(70, 35, 140, 140))
        p = New Pen(Color.Aquamarine, 3)
        e.Graphics.DrawEllipse(p, New Rectangle(65, 30, 150, 150))
    End Sub

And so to the code that will do the hard work for us and print the form to a printer:

Code Copy
  Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        ' Hide the button
        btnPrint.Visible = False
        '  Assign this form as the form to be printed
        PrintForm1.Form = Me

        '  Optional - Remove highlight from textbox
        '  (Otherwise it will be printed)
        TextBox1.SelectionStart = 0
        TextBox1.SelectionLength = 0

        '  Do the printing
        PrintForm1.Print()
        '  Redisplay Button
        btnPrint.Visible = True

    End Sub

I think you'll agree that it is very simple and the commenting in the code needs no further explanation.

Other Options
Although I have seen several good examples where a screenshot has been used to save a displayed form to file and the file is then printed, these all seem to suffer from the same problem - if the form is larger than the screen, only the visible area will be saved.
The PrintForm component handles this problem very effectively with the Scrollable PrintOption.

It is simple to use and if you want to print everything on the form - even if it is off the screen then this code syntax will do the job:

Code Copy
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

It really is as simple as that. The PrintOptions also include choices as to whether you print out the Title Bar or just the client area.

Altogether, this is a very useful package - well done the team who put this together to resolve a long running criticism of this particular VB.NET feature.

posted on Tuesday, June 19, 2007 12:08 PM

Feedback

# re: How To Print A Form To Printer in VB 2005 6/20/2007 4:19 PM Benny
Nice and easy. thank-you

# re: How To Print A Form To Printer in VB 2005 7/4/2007 11:24 AM 窃听器
Good ! thank the author.

# re: How To Print A Form To Printer in VB 2005 8/15/2007 3:20 PM Jo
Very neat. Just downloaded it and used it. Worth noting is the following
Drag a PageSetupDialog object onto your page and use it to alter the settings of your printer. then the code
PrintForm1.printersettings = PageSetUp1.printersettings
transfers the settings to your printForm printing. Very useful for landscape I found


# re: How To Print A Form To Printer in VB 2005 9/1/2007 9:51 AM Ged
Good tip there, Jo! Thanks.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: