Printing in Vb 2010

Topics: Printing, Subroutine, Object-oriented programming Pages: 15 (4555 words) Published: February 28, 2012
This Bonus Chapter accompanies Mastering Visual Basic 2010, which is available from

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 PrintDocument component is required, and you will have to master the members of this control.

The PrintDocument Control
This object represents your printer, and you must add a PrintDocument control to any project that generates printouts. In effect, everything you draw on the PrintDocument object is sent to the printer. The PrintDocument object represents the printing device, and it exposes a Graphics object that represents the printing surface, just like the Graphics property of all Windows controls. You can program against the Graphics object by using all the graphics methods. If you can create drawings on a form, you can just as easily print them on your printer. To print text, for example, you must call the DrawString method. You can also print frames around the text with the DrawLine or DrawRectangle method. In general, you can use all the methods of the Graphics object to prepare the printout.



The PrintDocument control is invisible at runtime, and its icon will appear in the Components tray at design time. When you’re ready to print, call the PrintDocument object’s Print method. This method doesn’t produce any output, but it does raise the control’s BeginPrint and PrintPage events. The BeginPrint event is fired as soon as you call the Print method, and this is where you insert the printout’s initialization code. The PrintPage event is fired once for every page of the printout, and this is where you must insert the code that generates output for the printer. Finally, the EndPrint event is fired when the printout ends, and this is where you insert the code to reset any global variables. The following statement initiates the printing: PrintDocument1.Print

This statement is usually placed in a button’s or a menu item’s Click event handler. To experiment with simple printouts, create a new project, place a button on the form, add an instance of the PrintDocument object to the form, and enter the...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay about Printing
  • PHI 2010 Essay
  • Essay on 3d Printing
  • 3D Printing Research Paper
  • FIN 2010 Essay
  • Essay about Vb Final
  • 3D printing Essay
  • 3D Printing Essay

Become a StudyMode Member

Sign Up - It's Free