Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

416 Part III Designing the User Interface


The PrintDocument class is located in the System.Drawing.Printing namespace. The
System.Drawing.Printing namespace provides several useful classes for printing text and
graphics, including the PrinterSettings class, which contains the default print settings for
a printer; the PageSettings class, which contains print settings for a particular page; and the
PrintPageEventArgs class, which contains event information about the page that’s about to
be printed. The System.Drawing.Printing namespace is automatically incorporated into your
project. To make it easier to reference the printing classes and other important values in this
namespace, add the following Imports statement to the top of your form:

Imports System.Drawing.Printing

To learn how to use the PrintDocument class in a program, complete the following exercise,
which teaches you how to add a PrintDocument control to your project and use it to print
a graphics file on your system.

Use the PrintDocument control


  1. Start Microsoft Visual Studio, and then create a new Visual Basic Windows Forms
    Application project named My Print Graphics.
    A blank form opens in the Visual Studio Integrated Development Environment (IDE).

  2. Use the Label control to draw a label object near the top of the form.

  3. Use the TextBox control to draw a text box object below the label object.


The text box object will be used to type the name of the artwork file that you want to
open. A single-line text box will be sufficient.


  1. Use the Button control to draw a button object below the text box.


This button object will print the graphics file. Now you’ll add a PrintDocument control.


  1. Scroll down until you see the Printing tab of the Toolbox, and then double-click the
    PrintDocument control.
    Like the Timer control, the PrintDocument control is invisible at run time, so it’s placed
    in the component tray beneath the form when you create it. Your project now has
    access to the PrintDocument class and its useful printing functionality.

  2. Set the following properties for the objects on your form:


Object Property Setting
Label1 Text “Type the name of a graphic file to print .”
TextBox1 Text “C:\Vb10sbs\Chap15\Sun .ico”
Button1 Text “Print Graphics”
Form1 Text “Print Graphics”
Free download pdf