Printing1

Objectives


Discussion

PrintDocument Control

Pens, Brushes, and Fonts

Print Dialog Boxes

Back to top


Demonstration

In this demo we will show how to use the PrintDocument and PrintPreviewDialog controls.  Using the PrintDialog control is similar to using the PrintPreviewControl.  However, when we use the PrintPreviewControl we don't have to actually print the page on paper and so we won't waste so much paper in the lesson.

1.  Create a new project.  We will use this project in the next lesson.

2.  Add a menu to the form and two menu items: Print (mnuPrint) and PrintPreview (mnuPrintPreview).  Add a textbox (txtPrint) and make it multi-line.

3.  Add a PrintDocument control (PrintDocument1) and a PrintPreviewDialog control (dgPP).   The PrintDocument control is the heart of the whole printing operation.

4.  First we will use just the PrintDocument control to print something.  In the Click event for mnuPrint  add the following line of code:

Me.PrintDocument1.Print()

5.  If we run the program now and click Print, we will get a page with nothing on it.  That is because we have not told the PrintDocument control what to print.  Now we will add code to actually create the stuff that will be printed on the page.  The PrintPage event for the PrintDocument control is fired when the Print method is called.  We will use this method to actually create the page that will be printed.  Inside of the PrintPage event for PrintDocument1, type the following one line of code.  You should actually type it and see what information you are prompted for. 

e.Graphics.DrawString(txtPrint.Text, _
New Font("arial", 10), Brushes.Black, _
e.MarginBounds.Left, e.MarginBounds.Top)

6. This statement will print what is inside the textbox.  But you have to give information about how to print it (font, color) and where to print it.  In this case we are starting the printing at the left and top margins. You could type in specific coordinates if you want.  Run your program, type something in the textbox and click print.  Hopefully something is printed.

7.  Now we will use the PrintPreviewDialog box.  First we must link the PrintPreviewDialog box to PrintDocument1.  In the Properties Window for the dgPP , set the Document property to "PrintDocument1". Now add the following code to the click event for mnuPrintPreview:

If dgPP.ShowDialog = DialogResult.OK Then
   Me.PrintDocument1.Print()
End If

8.  Run your program and test out the PrintPreview menu item.  Notice that we can see what we will print without actually wasting the paper.  

Back to top
Exercises

1.  In this exercise modify your Unit7 Editor project as follows.  

  1. Add a PrintPreview item to the menu.  
  2. "Print" (using the PrintPreviewDialog control) the contents of the RichTextBox. 
  3. Ensure that the font of the printed text is the same as the font in of the RichTextBox.

 You can also complete this exercise without your Unit7 project.

2.  Create a form with 6 textboxes for first name, last name, address, city, zip, and state.  Use the PrintPreviewDialog control to print an address label in the following format:

FirstName LastName
Address
City, State Zip

Back to top
Links & Help
Back to top