Bonus Chapter 2
Printing with Visual Basic 2010
Evangelos Petroutsos
The topic of printing with Visual Basic is a not trivial, and many developers use third-party tools to add print capabilities to their applications. As you already know, there’s no control with built-in printing capabilities. It would be nice if certain controls, such as the TextBox or the ListView control, would print their contents, but this is not the case. Even to print a few text paragraphs entered by the user on a TextBox control, you must provide your own code. Printing with VB isn’t complicated, but it requires a lot of code — most of it calling graphics methods. You must carefully calculate the coordinates of each graphic element placed on the paper, take into consideration the settings of the printer and the current page, and start a new page when the current one is filled. It’s like generating graphics for the monitor, so you need a basic understanding of the graphics methods, even if you’re only going to develop business applications. If you need to generate elaborate printouts, I suggest that you look into third-party controls with built-in printing capabilities, because the controls that come with Visual Studio have no built-in printing capabilities. The examples of this tutorial will address many of your day-to-day needs, and I’m including examples that will serve as your starting point for some of the most typical printing needs, from printing tabular data to bitmaps.
The Printing Components
We’ll start our exploration of Visual Basic’s printing capabilities with an overview of the printing process, which is the same no matter what you print. In the following section, you’ll find a quick overview of the printing controls (you’ll find more information on them, as well as examples, in the following sections). You don’t need to use all these components in your project. Only the