XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

NovDecember 2006Jan
SMTWTFS
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

Archives

Topics

Ramblings

VB.NET

Wednesday, December 20, 2006 #

   So you want print something out to the printer, but because of the layout you need it to be printed Landscape?   You would think - as I did - that all you need do is add something like this to the Print code:

e.PageSettings.Landscape = True

  Do that and you won't get an Exception thrown.  Unfortunately you won't get Landscape printing either!  

  The fix for this is to use instead the following line in the code that you pass to the PrintPage method:

PrintDocument1.DefaultPageSettings.Landscape = True

   So to take an example that uses PrintDocument1 and causes the document to print when a button is pressed, you would have code like the following (this example prints a list from a listbox, but any printing task will do).

   First, the calling code:


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PrintDocument1.DefaultPageSettings.Landscape =
True
PrintDocument1.Print()
End Sub


   And then the printing code:


Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Xpos = 100
Ypos = 200
Dim MyFont As New Font("Arial", 12)
For count = 0 To Listbox1.Items.Count - 1
e.Graphics.DrawString(ListBox1.Items(count), MyFont, Brushes.Black, Xpos, Ypos)
Ypos += 25
Next
End Sub

  This is a strange little Gotcha that had me puzzled for some time.   Thanks to another VBCity member (eightyseventh) we now all have the answer.

posted @ 12:23 PM