XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Ramblings

VB.NET

   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 on Wednesday, December 20, 2006 12:23 PM