printing a text file, printdocument1_printpage gets called TWICE for each page?!

T

tghamm

Ok, so this is driving me mad. For some reason, regardless of the
value of ev.hasmorepages, the printoducment1_printpage gets called
twice for every page. So, I print 2 pages of data on one page, with
the second overlaying the original data. If i set ev.hasmorepages to
false, essentially telling the printdocument to only print one page,
it prints one page, with two pages of data, because at the end of the
_printpage subroutine, even when ev.hasmorepages is false, it runs the
sub once more. Any help on this would be fantastic, below is my code:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
'ev.HasMorePages = True
' Calculate the number of lines per page.
linesPerPage = (ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)) - 1

' Print each line of the file.
'If counterx = False Then
While count < linesPerPage
line = streamtoprint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count *
printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, New StringFormat())
count += 1
End While
'End If
'If counterx = False Then
'counterx = True
'ElseIf counterx = True Then
'counterx = False
'End If
' If more lines exist, print another page.
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub

...........THIS IS WHAT CALLS THE PRINTPAGE....
streamtoprint = New IO.StreamReader("C:\temp.dat")
Try
printFont = New Font("Times New Roman", 10)
AddHandler PrintDocument1.PrintPage, AddressOf
Me.PrintDocument1_PrintPage
PrintDocument1.Print()
Finally
streamtoprint.Close()
IO.File.Delete("C:\temp.dat")
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Thanks everyone!
 
T

tghamm

Solution!
The problem was this line:
AddHandler PrintDocument1.PrintPage, AddressOf
Me.PrintDocument1_PrintPage

I'm not entirely sure why this creates a problem, but I think because
I'm using a windows form designer printdocument, that line actually
creates a second thread during the spooling process and makes the
event fire twice. This si the single wierdest thing I've had happen
in VB so far, but there ya go.
 
C

Chris Dunaway

Solution!
The problem was this line:
AddHandler PrintDocument1.PrintPage, AddressOf
Me.PrintDocument1_PrintPage

I'm not entirely sure why this creates a problem, but I think because
I'm using a windows form designer printdocument, that line actually
creates a second thread during the spooling process and makes the
event fire twice. This si the single wierdest thing I've had happen
in VB so far, but there ya go.

It's got nothing to do with threads, it looks like you just wired up
the event twice. I assume that, elsewhere in your code, you have this
method defined (with the Handles clause):

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

The handles keyword wires up the PrintPage event to this method. When
you added the AddHandler call, you wired it up a second time. So each
time the event fires, it calls it twice.

If you use the Handles keyword, you don't need to use AddHandler. If
you remove the Handles keyword, then you need to use AddHandler to
wire up the event.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top