PrintForm
PrintForm provides a very simple way of printing a hard copy of a form's image. The PrintForm statement was available in classic VB, but was left out of the original version of VB.NET. However, around the time of the VB 2005 release, Microsoft made a set of components available called "Visual Basic Power Packs", which resurrected some of the features from classic VB that were omitted in the original version of VB.NET. For VB 2005, the Power Packs component set had to be downloaded and installed separately; however the 2008 and 2010 releases have this already bundled in.
The sample application prints the image of the form shown below (without the Print and Exit buttons). The program does not do any calculations; all the numbers you see there are static text. The only processing the program does is set the date and time when loaded, print the form when the Print button is clicked, and closes the form when the Exit button is clicked.
When the program is run, the user will initially see this:
When the user clicks the Print button, the form's image will be printed on the default printer. Note that the Print and Exit buttons are not displayed (this is because the code in the program hides the buttons before printing the form, then redisplays them after):
Some things worth noting when building this project:
To use PrintForm, you must locate the PrintForm control in the Toolbox, under "Visual Basic Power Packs". |
|
Once you double-click the PrintForm control in the Toolbox, it will appear in a gray bar underneath the form. This gray bar is an area where non-visual controls used by your application will appear (other non-visual controls include timers and file dialog boxes). It is at this point that PrintForm can be used. |
|
In the Click event procedure for the Print button (named cmdPrint), we see the statement PrintForm1.Print, which causes the form to print. |
|
Download the project files for this sample application here.