PrintHelper VB Class

by Bill Dowling 29. July 2010 09:33

Printing in a .Net (or any VB) program has always been a challenge. I developed the following VB.Net Class (with guidance from others) to help simplify the printing process.

The class can be created with;

  • Dim ph As New PrintHelper(_event, _font, _debug)

    _event As System.Drawing.Printing.PrintPageEventArgs From the Event Handler
    _font As Font (optional) Default Font
    _debug As Boolean (optional) Debug Flag (draw boxes around text) - Default is False

Text can be printed by calling one of the following routines. If any field is not included in the call, the default value will be used in the class;

  • ph.Print(_text, _UpdateX)
  • ph.Print(_text, _x, _y, _UpdateX)
  • ph.Print(_text, _font, _UpdateX)
  • ph.Print(_text, _font, _x, _y, _UpdateX)
  • ph.Print(_text, _font, _sf, _UpdateX)
  • ph.Print(_text, _font, _sf, _y, _UpdateX)
  • ph.Print(_text, _font, _sf, _x, _y, _UpdateX)

    _text As String String to print
    _font As Font Font to use for this string
    _sf As StringFormat StringFormat to left/right/center justify the text
    _x As Single X or horizontal position on the page to begin printing
    _y As Single Y or vertical position on the page to begin printing
    _UpdateX As Boolean (optional) Update X Coordinate after Print - Default is False

If no X position is given, the string will begin at the left margin unless _UpdateX option was used on prior Print statement. The _UpdateX option will reset the default X position to the end of the current Text printed. Don't forget to reset default X position by using xPos property.

If no Y position is given, the current vertical position will be used, which is updated after each line is printed.

Additional Properties or Functions ;

Property SetFont() As Font Set the default Font
Property FontHeight() As Single Get or Set the default Font Height
Property SetStringFormat As StringFormat Set the default StringFormat
Property SetBrush() As Brush Set the default Brush
Property xPos() As Single Get or Set the current default X position
Property yPos() As Single Get or Set the current default Y position
Routine AddBlankLine() Increase Y position by FontHeight
Routine PrintBox(_text, _font, x, y, _width, _height) Draw a box around text rectangle
Routine PrintLine(_font, x, y, _width) Draw a Line

The PrintHelper.vb Class: Updated 7/28/2011

This code is used to Print Text to a printer within your code:


.Net Coding Examples

Add comment

About the author

Bill Dowling has been a VB and .Net programmer for as long as he can remember.

Month List