PC Review


Reply
Thread Tools Rate Thread

Using printdocument - prints over itself with no consistency

 
 
tghamm@gmail.com
Guest
Posts: n/a
 
      8th May 2007
Hey everyone, I'm trying to output a text file to a printer, and for
some reason, it prints over itself, something that should be 15 pages
can be from 4-8 pages, and it changes each time I try it. Something
is definitely wrong.
I checked the printpage event, it gets called all 15 times for the 15
pages of data, but the number of pages spooling doesnt change at the
same pace. I dont get it.
Below is my code, any ideas why this would happen?


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

Then the Printpage arguement

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

' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)

' Print each line of the file.
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

' If more lines exist, print another page.
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub

I've been programming for over a decade, and this is the first time
I;ve ever been so stumped I had to post my code. Any help would be
truly appreciated.

 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      9th May 2007
Hi,

either remove the line 'AddHandler PrintDocument1.PrintPage, AddressOf
Me.PrintDocument1_PrintPage'
or the 'handles' part of the 'Private Sub PrintDocument1_PrintPage and the
problem should go away.

HTH



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hey everyone, I'm trying to output a text file to a printer, and for
> some reason, it prints over itself, something that should be 15 pages
> can be from 4-8 pages, and it changes each time I try it. Something
> is definitely wrong.
> I checked the printpage event, it gets called all 15 times for the 15
> pages of data, but the number of pages spooling doesnt change at the
> same pace. I dont get it.
> Below is my code, any ideas why this would happen?
>
>
> 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
>
> Then the Printpage arguement
>
> 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
>
> ' Calculate the number of lines per page.
> linesPerPage = ev.MarginBounds.Height /
> printFont.GetHeight(ev.Graphics)
>
> ' Print each line of the file.
> 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
>
> ' If more lines exist, print another page.
> If (line IsNot Nothing) Then
> ev.HasMorePages = True
> Else
> ev.HasMorePages = False
> End If
> End Sub
>
> I've been programming for over a decade, and this is the first time
> I;ve ever been so stumped I had to post my code. Any help would be
> truly appreciated.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PowerPoint keeps crashing, over and over and over and over MB Microsoft Powerpoint 1 12th Jan 2010 08:00 PM
BCM shutting down over and over and over and over TheMurf Microsoft Outlook BCM 1 9th Mar 2007 03:32 PM
Getting same message over and over and over and over each day =?Utf-8?B?Z29pbmJhdHR5?= Microsoft Outlook 1 18th Nov 2005 10:52 PM
XP Login Over & Over & Over & Over Desert Rat Windows XP Help 1 30th Oct 2004 02:37 AM
Outlook 2002 crashes over and over and over and over and over and over typoo Microsoft Outlook Discussion 5 7th Mar 2004 08:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:03 PM.