Bagikan melalui


PrintForm Component (Visual Basic)

The PrintForm component for Visual Basic 2008 enables you to print an image of a Windows Form at run time. Its behavior replaces that of the PrintForm method in earlier versions of Visual Basic.

PrintForm Component Overview

A common scenario for Windows Forms is to create a form that is formatted to resemble a paper form or a report, and then to print an image of the form. Although you can use a PrintDocument component to do this, it would require a lot of code. The PrintForm component enables you to print an image of a form to a printer, to a print preview window, or to a file without using a PrintDocument component.

The PrintForm component is located on the Visual Basic PowerPacks tab of the Toolbox. When it is dragged onto a form it appears in the component tray, the small area under the bottom border of the form. When the component is selected, properties that define its behavior can be set in the Properties window. All of these properties can also be set in code. You can also create an instance of the PrintForm component in code without adding the component at design time.

When you print a form, everything in the client area of the form is printed. This includes all controls and any text or graphics drawn on the form by graphics methods. By default, the form's title bar, scroll bars, and border are not printed. Also by default, the PrintForm component prints only the visible part of the form. For example, if the user resizes the form at run time, only the controls and graphics that are currently visible are printed.

The default printer used by the PrintForm component is determined by the operating system's Control Panel settings.

After printing is initiated, a standard PrintDocument printing dialog box is displayed. This dialog box enables users to cancel the print job.

Key Methods, Properties, and Events

The key method of the PrintForm component is the Print method, which prints an image of the form to a printer, print preview window, or file. There are two versions of the Print method:

  • A basic version without parameters: Print()

  • An overloaded version with parameters that specify printing behavior: Print(printForm As Form, printFormOption As PrintOption)

    The PrintOption parameter of the overloaded method determines the underlying implementation used to print the form, whether the form's title bar, scroll bars, and border are printed, and whether scrollable parts of the form are printed.

The PrintAction property is a key property of the PrintForm component. This property determines whether the output is sent to a printer, displayed in a print preview window, or saved as an Encapsulated PostScript file. If the PrintAction property is set to PrintToFile, the PrintFileName property specifies the path and file name.

The PrinterSettings property provides access to an underlying PrinterSettings object that enables you to specify such settings as the printer to use and the number of copies to print. You can also query the printer's capabilities, such as color or duplex support. This property does not appear in the Properties window; it can be accessed only from code.

The Form property is used to specify the form to print when you invoke the PrintForm component programmatically. If the component is added to a form at design time, that form is the default.

Key events for the PrintForm component include the following:

  • BeginPrint event. Occurs when the Print method is called and before the first page of the document prints.

  • EndPrint event. Occurs after the last page is printed.

  • QueryPageSettings event. Occurs immediately before each page is printed.

Remarks

If a form contains text or graphics drawn by Graphics methods, use the basic Print (Print()) method to print it. Graphics may not render on some operating systems when the overloaded Print method is used.

If the width of a form is wider than the width of the paper in the printer, the right side of the form might be cut off. When you design forms for printing, make sure that the form fits on standard-sized paper.

Example

The following example shows a common use of the PrintForm component.

' Visual Basic.
Dim pf As New PrintForm
pf.Form = Me
pf.PrintAction = PrintToPrinter
pf.Print()

See Also

Tasks

How to: Print a Form by Using the PrintForm Component (Visual Basic)

How to: Print the Client Area of a Form (Visual Basic)

How to: Print Client and Non-Client Areas of a Form (Visual Basic)

How to: Print a Scrollable Form (Visual Basic)

Reference

Print

PrintAction