Printing With Visual Basic  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Visual Basic
Written by James Crowley   
Thursday, 15 March 2007

{mos_sb_discuss:39}   

Despite claims that computers would create a 'paperless society', printing is still one of the most important functions of an application. It is also one of the most common. Although VB makes it easy to print, it's not one of VB strengths.



This tutorial will introduce you to the functions VB does provide for printing. By the end of this article you should be able to print text, shapes and even complete images files with ease. You should also be able to set page formatting options, work out which printer the output will be directed to, etc.

Unsurprisingly, Visual Basic provides us with the Printer and Printers objects, which are available at runtime to all VB apps. The Printers object gives us information about all the available printers installed on the system. The Printer object controls all the output to the active printer and retrieves information such as the printer name.

First of all, lets look at the basic steps involved in printing with VB. First off, we send all the data we want to print using the Print method. For example, if we wanted to print "Hello", we would call

Printer.Print "Hello"

Each time you call the Print method, your output will move to the next line. So,

Printer.Print "Hello"

Printer.Print "Goodbye!"


would appear as

Hello

Goodbye!


on paper.

If you want to output a new page, you simply call the NewPage method:

Printer.NewPage

Once you have finished outputting the data to the printer, you need to call the EndDoc method:

Printer.EndDoc

Windows will then spool your document to the printer.

Setting formatting options

Using the Printer object further, you can also apply formatting to the text you output by setting the Font and its associated FontSize, FontBold, FontItalic, FontStrikeThru and FontUnderline properties. To change the font, simply change the FontName property. For example,

Printer.FontName = "Tahoma"

changes the font to Tahoma. To change the size, just call Printer.FontSize. Most of the other formatting options are just boolean (ie True or False), which makes it even easier to apply formatting. The code below gives a simple demonstration:

With Printer

Printer.Print "Normal Line"

.FontBold = True

Printer.Print "Bold Line"

.FontItalic = True

Printer.Print "Bold and Italic Line"

.FontBold = False

.FontItalic = False

Printer.Print "Second Normal Line"

.EndDoc

End With


Although I am using a With statement, I still have to write Printer.Print for outputting the text. This is another 'quirk' of VB - the Print statement does not actually belong to the Printer object, or in fact any other object in VB. If you search for Print in the Object Browser (press F2), you won't find it anywhere. Print is in fact a statement that VB interprets on its own, and outputs it according to the object before the Print statement. If you call PictureBox1.Print, it will 'print' the text into the PictureBox control. As such, the Print function behaves just as if it were an object of PictureBox or Printer. However, the designers of VB obviously didn't extend this behavior to With statements.

For a full list of font options, go to the Object Browser (F2), and select Printer from the Classes list.

One other thing worth noting regarding formatting... If you are using the RichTextBox control, you don't need to worry about applying all the formatting - there is some code using API calls which does it all for you, including page margins! Click here to see that code.

Read more


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Sunday, 08 July 2007 )
 
< Prev   Next >