XTab's Blog

Ged Mead's Blog at vbCity

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

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

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