- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary
726 Chapter 15 PRINTING WITH VB.NET
The PrintTable project is the starting point for tabular reports, and it demonstrates the core of an application that prints tables. You will have to add a title to each page, a header and a footer for each page (with page numbers and dates), and quite possibly a grid to enclose the cells. Experiment with the PrintTable project by adding more features to it. You can become as creative as you wish with this application. I should also bring to your attention the fact that the PrintTable application ends the page when the report’s height exceeds the 95 percent of the page’s printable area. This test takes place after printing each item. If the last title printed on a page has a dozen different authors, it will run over the bottom of the page.
Using Static Variables
The PrintPage event handler produces all the pages, one after the other. These pages, however, are not independent of one another. When you print a long text file, for example, you must keep track of the pages printed so far or the current line. If you set up a variable that keeps track of the current line, you shouldn’t reset this variable every time the PrintPage event handler is executed. One way to maintain the value of a variable between consecutive calls of the same procedure is to declare it with the Static keyword. Static variables maintain their values between calls, unlike the private variables.
In the PrintTable project, I’ve used the itm variable to keep track of the item being printed. Each time a page is filled, the PrintPage event handler is terminated and we start printing on a new page. By making the variable itm static, we’re sure that it won’t be reset every time the PrintPage event handler is entered. Instead, all items are printed sequentially on one or more pages.
Printing Plain Text
In this section we’ll examine a less-exciting operation, the printing of a text file. It should be a trivial task after the program that prints the tabular reports, but it’s not nearly as trivial as you may think. But why bother with a simple operation like printing plain text? The reason is that no control has built-in printing capabilities, and text files are still quite common. Printing formatted text is even more complicated, so we’ll start with plain text files.
Plain text means that all characters are printed in the same font, size, and style, just like the text you enter on a TextBox control. Your task is to start a new page when the current page fills and break the lines at, or before, the left margin. Because the text is totally uniform, you know in advance the height of each line and you can easily calculate the number of lines per page ahead of time.
If you look up the printing examples that come with Visual Studio, they’re all based on the assumption that each text line fits across the page. As we all know, this is rarely true. The sample code provided simply dumps each text line to the printer and is much faster than the code you’ll find in this section, but it’s of little practical use and here’s why. Text files are made up of lines, which may exceed by far the width of the page. It’s your responsibility to break the long lines into shorter ones and fit them on the page. As you know, each paragraph in a text file is a separate line and rarely fits on a single line on the printed page. The TextBox control, for example, breaks lines according to its width, and if you resize the control at runtime, the line breaks will be recalculated. Your code must do exactly that: calculate the proper page breaks, so that the lines will fit on the page.
Note The PrintText project discussed in the following section takes approximately 10 seconds to prepare a page of text on an 850 MHz computer. Following the discussion of the PrintText project, you will find a much faster technique for printing text, but it’s not as flexible as the methods described in the following section.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
PRINTING EXAMPLES 727
VB.NET at Work: The PrintText Project
In this section, we’re going to build the PrintText application. The main form of this application contains a TextBox control and a button that prints the text on the control. The program displays a preview of the text on a PrintPreview dialog box, and you can print the text by clicking the Print button on the dialog box. Figure 15.9 shows a section of a text previewed with the PrintText application.
Figure 15.9
Printing and previewing documents with the PrintText application
You can use the TextBox control to enter a few paragraphs or a few pages of text and then print them. Before we examine the code, let me describe shortly the process of breaking the text into lines that fit across the page. We’ll start reading the text one word at a time (including white space, punctuation, and line feeds) and calculate the length of the word when printed on paper. If the length of the current line plus the length of the word don’t exceed the width of the printable area, we’ll append the word to the string and continue. At each step we’ll call the MeasureString method to calculate the length of the string when printed. When this string exceeds the width of the printable area of the page, we’ll print the string and create a new string with the word that wouldn’t fit on the previous line, and then we’ll start over again. The string will grow by one word at a time, until it’s just right to be printed.
Let’s start with the function that extracts the next word of the document on the TextBox control. This is the GetNextWord() function, which reads consecutive words from a text file and slowly builds a string by appending each word to the string. When the length of the string plus the length of the next word exceed the width of the page (excluding the margins, of course), we print the line and start a new line with the last word. The process continues until all the words in the document are exhausted. The GetNextWord() function, shown in Listing 15.9, keeps track of the current location in the document, so that it can read the next word every time it’s called. To do so, it uses the currPosition variable, which is declared as Static. This variable is the index of the next word’s first character in the text.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
728 Chapter 15 PRINTING WITH VB.NET
Listing 15.9: The GetNextWord() Function
Function GetNextWord(Optional ByVal reset As Boolean = False) As String Static currPos As Integer
Dim word As String
If reset Then currPos = 0
If currPos >= TextBox1.Text.Length Then Return “”
While Not System.Char.IsLetterOrDigit(TextBox1.Text.Chars(currPos)) word = word & TextBox1.Text.Chars(currPos)
currPos = currPos + 1
If currPos >= TextBox1.Text.Length Then Return word End While
While Not (System.Char.IsWhiteSpace(TextBox1.Text.Chars(currPos))) word = word & TextBox1.Text.Chars(currPos)
currPos = currPos + 1
If currPos >= TextBox1.Text.Length Then Return word End While
Return word End Function
The GetNextWord() function accepts an optional argument, the reset argument. This argument forces the function to start reading words from the beginning of the text. Otherwise, you wouldn’t be able to reuse it in the same session. After scanning the text once, it would return the empty string, indicating that there are no more words to be read. This function encapsulates all the complexity of isolating words and their surrounding spaces from the rest of the application, and you can use it in your applications in similar situations. However, you will have to modify it a little. This implementation of the GetNextWord() function doesn’t ignore spaces and punctuation marks. In printing a document, all characters are significant, and you can’t afford to ignore a single space. To extract individual words from a text document, you should treat punctuation marks as delimiters and ignore them, as well as trim all the spaces around the words.
We must also keep track of the height of the text, so that we can start a new page when needed. To start a new page we simply set the e.HasMorePages argument to True and exit the PrintPage event handler. The PrintDocument object will fire another PrintPage, in which we’ll print a new page, and we’ll continue until we run out of words.
To initiate the printout, the Print button contains the following statements:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
GetNextWord(True)
End Sub
Now we can focus on the code that creates the actual printout, which is shown in Listing 15.10. The code reads a word at time, adds its length to the length of the current line, and then compares it to the width of the printable area of the page. If the total length fits on the page, we proceed with the next word. If not, we print the line, advance vertically by a little more than the line’s height, and
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
PRINTING EXAMPLES 729
start a new line with the last word that didn’t make it on the line just printed. This technique assumes that the text doesn’t contain words that can’t fit on the width of the printable area of the page. You must add your own code to arbitrarily break such awfully long lines.
Listing 15.10: The PrintText Project’s PrintPage Event Handler
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage
Dim txtFont As New Font(“Arial”, 10) Dim txtH As Integer = _
PrintDocument1.DefaultPageSettings.PaperSize.Height - _ PrintDocument1.DefaultPageSettings.Margins.Top - _ PrintDocument1.DefaultPageSettings.Margins.Bottom
Dim LMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Left Dim TMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Top Dim txtW As Integer = _
PrintDocument1.DefaultPageSettings.PaperSize.Width - _ PrintDocument1.DefaultPageSettings.Margins.Left - _ PrintDocument1.DefaultPageSettings.Margins.Right
Dim linesPerPage As Integer = _
e.MarginBounds.Height / txtFont.GetHeight(e.Graphics) Dim R As New RectangleF(LMargin, TMargin, txtW, txtH) Static line As String
Dim word As String
Dim cols, lines As Integer word = GetNextWord()
While word <> “” And lines < linesPerPage line = line & word
word = GetNextWord()
e.Graphics.MeasureString(line & word, txtFont, New SizeF(txtW, txtH), _ New StringFormat(), cols, lines)
End While
If word = “” And Trim(line) <> “” Then e.Graphics.DrawString(line, txtFont, Brushes.Black, R, _
New StringFormat())
e.HasMorePages = False Exit Sub
End If
e.Graphics.DrawString(line, txtFont, Brushes.Black, R, New StringFormat()) e.HasMorePages = True
line = word End Sub
When the PrintPage event handler starts executing, it calculates the page’s geometry (the margins and the dimensions of the printable area of the page), as well as the number of text lines that can fit on the page (variable linesPerPage). Then, it starts calling the GetNextWord() function from within a
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
730 Chapter 15 PRINTING WITH VB.NET
loop, which terminates when the printed text exceeds the height of the page (minus the margins, of course). Every time a new page is filled, it sets the e.HasMorePages property to True and then exits.
When the GetNextWord() function runs out of words, which means we’ve reached the end of the text, it returns an empty string and the PrintPage event handler sets the e.HasMorePages property to False and exits.
As you can see, the most complicated part of the application is the retrieval of the next word to be printed. By implementing the GetNextWord() function and inserting all the logic of isolating words—including their surrounding space and punctuation—into this function, we simplified the logic of the PrintPage event handler.
To experiment with the printing methods, you can insert the code to print the document’s name and the current page number at the top of the page, or you can place a rectangle around the text. For an interesting effect, you can use a larger font, space vertically the text lines, and draw a light gray line under each text line. You can also insert the code presented earlier in this chapter to handle both landscape and portrait orientation.
You can also adjust the code to handle program listings. Program listings are plain text files, like the ones you can print with this application, but you must mark long code lines that are broken to fit on the page. You can insert a special symbol either at the end of a code line that continues on the following line on the page or in front of the continued line. This symbol is usually a bent arrow that resembles the Return key.
The PrintText project doesn’t send its output directly to the printer; instead, it generates a preview that allows you to examine the printout on the screen until you get your code right. You will notice that the generation of the preview is a slow process. There are many things you can do to speed up the code. For example, you can read several words at a time. It’s very unlikely that a line contains fewer than five or six words, so why waste time calculating the width of a very short line? You can also estimate the number of words that will fit on a page, read as many words, and then calculate how many lines they will take on the printed page with the MeasureString method. If they all fit on the page, you can add one word at a time (as we did earlier) until you fill the page. If the printed text exceeds the height of the page, you must subtract words until you fill the page, but no more. This is a fairly complicated logic that would overwhelm the discussion of the basic printing objects. Once you understand how the text is broken into lines and how to figure out when to change pages, you can make the algorithms as complicated as you wish.
VB.NET at Work: The PrintText2 Project
The PrintText project is fairly slow. It takes just over 10 seconds to prepare each page, which is quite slow compared to printing text with Notepad or even WordPad. Some readers may have already guessed how to speed up the printing of plain text. Why use the MeasureString method on individual words to find out whether they fit on the current line; why not pass the entire text as argument to this method, along with a rectangle that represents the printable area of the page? The method will return the number of characters that can be printed on the page, and then we can repeat the process with the rest of the text. Indeed, this is the way to print straight text.
The idea is to instantiate a Rectangle object that represents the printable area of the page. Then call the MeasureString method to find out how many characters will fit into the rectangle and print so many characters with the DrawString method. Just two method calls, and the first page is ready. Repeat the same process for the following pages, starting with the character following the last character printed
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
PRINTING EXAMPLES 731
on the previous page. This method is fast and works with all text files. However, it’s not as flexible as the method that prepares the printout one word at a time—I’ll discuss the drawbacks of this method at the end of the section.
The text to be printed is stored in the textToPrint variable, which is declared outside any procedure with the statement
Dim textToPrint As String
To make the application more flexible, I’ve added a Page Setup dialog box, where users can specify the margins and the orientation of the printout. The code starts by displaying the Page Setup dialog box by calling the ShowDialog method of the PageSetupDialog control. Then it initiates printing on an instance of the PrintPreviewDialog control, by calling its ShowDialog method. Listing 15.11 shows the code behind the Preview & Print button on the form, which initiates the printing.
Listing 15.11: Initiating the Printing of Plain Text
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click textToPrint = TextBox1.Text
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings If PageSetupDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings End If
Try
PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog()
Catch exc As Exception
MsgBox(“Print operation failed “ & vbCrLf & exc.Message) End Try
End Sub
The ShowDialog method of the PrintPreviewDialog control is equivalent to calling the Print method of the PrintDocument control. After that, a series of PrintPage events will follow. Listing 15.12 shows the code in the PrintPage event’s handler.
Listing 15.12: A Simpler Method of Printing Plain Text
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage
Static currentChar As Integer
Dim txtFont As New Font(“Arial”, 10) Dim txtH, txtW As Integer
Dim LMargin, TMargin As Integer
With PrintDocument1.DefaultPageSettings
txtH = .PaperSize.Height - .Margins.Top - .Margins.Bottom
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
732 Chapter 15 PRINTING WITH VB.NET
txtW = .PaperSize.Width - .Margins.Left - .Margins.Right LMargin = PrintDocument1.DefaultPageSettings.Margins.Left TMargin = PrintDocument1.DefaultPageSettings.Margins.Top
End With
If PrintDocument1.DefaultPageSettings.Landscape Then Dim tmp As Integer
tmp = txtH txtH = txtW txtW = tmp
End If
Dim linesperpage As Integer = txtH / txtFont.Height Dim R As New RectangleF(LMargin, TMargin, txtW, txtH) Dim lines, chars As Integer
Dim fmt As New StringFormat(StringFormatFlags.LineLimit) e.Graphics.MeasureString(Mid(textToPrint, currentChar + 1), _
txtFont, New SizeF(txtW, txtH), fmt, chars, lines) e.Graphics.DrawString(Mid(textToPrint, currentChar + 1), _
txtFont, Brushes.Black, R, fmt) currentChar = currentChar + chars
If currentChar < textToPrint.Length Then e.HasMorePages = True
Else
e.HasMorePages = False currentChar = 0
End If End Sub
The core of the printing code is concentrated in the following three statements:
e.Graphics.MeasureString(Mid(textToPrint, currentChar + 1), _
txtFont, New SizeF(txtW, txtH), fmt, chars, lines) e.Graphics.DrawString(Mid(textToPrint, currentChar + 1), _
txtFont, Brushes.Black, R, fmt) currentChar = currentChar + chars
The first statement determines how many characters will fit in a rectangle with dimensions txtW and txtH when rendered on the page in the specified font. The fmt argument is crucial for the proper operation of the application, and I will explain it momentarily.
The second statement prints the segment of the text that will fit in this rectangle. Notice that the code is using the Mid() function to pass not the entire text, but a segment starting at the location following the location of the last character in the previous page. The location of the first character in the page is given by the currentChar variable, which is increased by the number of characters printed on the current page. The number of characters printed on the current page is retrieved by the MeasureString method and stored in the chars variable.
And the trick that makes this code work is how the fmt StringFormat object is declared. The height of the printable area of the page may not (and usually does not) accommodate an integer number of lines. The MeasureString method will attempt to fit as many text lines in the specified
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |