Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
Chapter 17 Quick Reference
To | Do this |
Incorporate the PrintDocument class in your projects and prepare for printing | Add the following Imports statement to the top of your form:
ImportsSystem.Drawing.Printing |
Create a printing event handler | 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 handler | Use the Graphics.DrawImage method. For example:
ev.Graphics.DrawImage(Image.FromFile _ (TextBox1.Text), ev.Graphics.VisibleClipBounds) |
Print text from a printing event handler | Use the Graphics.DrawString method in an event handler. For example:
ev.Graphics.DrawString(TextBox1.Text, _ New Font("Arial", 11, FontStyle.Regular), _ Brushes.Black, 120, 120) |
Call a printing event handler | 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 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. |