Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 17 Working with Printers 437


Chapter 17 Quick Reference


To Do This
Make it easier to
reference the printing
classes in your projects

Add the following Imports statement to the top of your form:
Imports System.Drawing.Printing

Create a printing event
procedure

Double-click the PrintDocument1 object in the component tray
or
Use the AddHandler statement and the AddressOf operator. For example:
AddHandler PrintDocument1.PrintPage, _
AddressOf Me.PrintGraphic
Create a PrintDocument
object in your project

Double-click the PrintDocument control on the Printing tab of the Toolbox.
or
Include the following variable declaration in your program code:
Dim PrintDoc As New PrintDocument
Print graphics from
a printing event
procedure

Use the Graphics.DrawImage method. For example:
ev.Graphics.DrawImage(Image.FromFile _
(TextBox1.Text), ev.Graphics.VisibleClipBounds)
Print text from
a printing event
procedure

Use the Graphics.DrawString method in an event procedure. For example:
ev.Graphics.DrawString(TextBox1.Text, _
New Font("Arial", 11, FontStyle.Regular), _
Brushes.Black, 120, 120)
Call a printing event
procedure

Use the Print method of an object of type PrintDocument. For example:
PrintDoc.Print()
Print multipage text
documents

Write a handler for the PrintPage event, which receives an argument of
the type PrintPageEventArgs. Compute the rectangular area on the page
for the text, use the MeasureString method to determine how much text
will fit on the current page, and use the DrawString method to print the
text on the page. If additional pages are needed, set the HasMorePages
property to True. When all text has been printed, set HasMorePages
to False.
Open a text file by
using the FileStream
class, and then load
it into a RichTextBox
object

Create a variable of type FileStream, specifying the path and file mode,
load the stream into a RichTextBox, and then close the stream. For
example:
Imports System.IO 'at the top of the form
...
Dim MyFileStream As New FileStream( _
FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
Display printing dialog
boxes in your programs

Use the PrintDialog, PrintPreviewDialog, and PageSetupDialog controls
on the Printing tab of the Toolbox.
Free download pdf