473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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!

May 9 '07 #1
2 6410
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.


On May 9, 2:53 pm, tgh...@gmail.com wrote:
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!


May 9 '07 #2
On May 9, 2:21 pm, tgh...@gmail.com wrote:
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

May 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: freddy | last post by:
I am new to c#, I am trying to print the context of a textbox or a combobox.
4
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
3
by: islay | last post by:
Hello, I'm trying to print more than one page using the PrintDocument object. I set "e.HasMorePages = True" after completeing the first page in the "PrintDocument1_PrintPage" subroutine, and...
6
by: randy1200 | last post by:
I'm using Visual Studio 2005 and C#. I need to print a WinForm used for data entry as a graphic. In other words, I need to print the exact WinForm the user sees on the screen. I searched through...
3
by: Richard MSL | last post by:
I have an application that prints documents that it creates. It uses what I believe is a standard .NET way of doing so, like this: PrintDocument pd = new PrintDocument(); pd.PrintPage += new...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
2
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter...
3
by: bmerlover | last post by:
After hours of trying I got it to print and print preview the richtextbox. I've been trying to figure out how to print multiple pages. If the richtextbox contains more than 1 page of data, than it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.