printing multiple pages

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

Hi

I am trying to get my listbox items to print if they stream past the one
page mark. my code is working for one page of information (if the
e.hasmorepages) is not there.

But I am having trouble getting it to print on multiple pages. The second
page will not print at all.

Thanks in advance

Bill


'-----------------------------------------------------------------

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

'-----------------------------------------------------------------

Dim lpp As Single

Dim yPos As Single

Dim LeftMargin As Single = e.MarginBounds.Left

Dim TopMargin As Single = e.MarginBounds.Top

Dim line As String

Dim printFont As Font

printFont = New Font("Courier New", 10)

lpp = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

While (count < lpp And count < NumofLines)

yPos = TopMargin + (count * printFont.GetHeight(e.Graphics))

e.Graphics.DrawString(lstAddTape.Items(count), printFont, Brushes.Black,
LeftMargin, yPos)

count = count + 1


End While

If (count < NumofLines) Then

e.HasMorePages = True

PrintDocCalc.Print()

Else

e.HasMorePages = False

End If



End sub
 
Bill,
Take out the PrintDocCalc.Print() call after setting HasMorePages. Also
count needs to be reset to 0 at the start of each call for proper
positioning. If you want to use this (presumed) global to count the
position in the list box then have a line # variable that gets initialized
and incremented in the print routine. The current code would just start the
second page at the bottom margin and continue from there. It might just
hang as count never gets incremented after it hits lpp and you don't show
any code that resets it.

Ron Allen
 
Hi Ron

I guess my bigest question would be how are to to call the second page.
Because if put the if statement at the end the number of pages just keep
racking up without printing. caught in an undless loop. if I put the if
statement inside the while statement to go and continue with the while
statement it just hangs and does not print.

Bill



If (count < NumofLines) Then

e.HasMorePages = True

count = 0

Else

e.HasMorePages = False

End If

'-----------------------------------------------------------------

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

'-----------------------------------------------------------------



Dim lpp As Single

Dim yPos As Single

Dim LeftMargin As Single = e.MarginBounds.Left

Dim TopMargin As Single = e.MarginBounds.Top

Dim line As String

Dim printFont As Font

printFont = New Font("Courier New", 10)

lpp = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

While (count < lpp And count < NumofLines)

yPos = TopMargin + (count * printFont.GetHeight(e.Graphics))

e.Graphics.DrawString(lstAddTape.Items(count), printFont, Brushes.Black,
LeftMargin, yPos)

count = count + 1


End While

If (count < NumofLines) Then

e.HasMorePages = True

count = 0

Else

e.HasMorePages = False

End If

End Sub
 
Bill,
You need two seperate variables to track 1) the position in the list box
(currently count) and 2) the line position on the page (currently the same
thing). So at the beginning of the routine use
Dim lno AS Integer
lno = 0
....... then
While (lno < lpp) AND (count < NumofLines)
yPos = TopMargin + (lno * printFont.GetHeight(e.Graphics))
e.Graphics.DrawString(lstAddTape.Items(count), printFont, Brushes.Black,
LeftMargin, yPos)
lno = lno + 1
count = count + 1
End While

Don't reset count in the routine.
I'd also make lpp an integer as CInt(e.MarginBounds.Height / fontHeight)
for efficiency and compute the font height just once at the beginning rather
than each time a line is printed.
Dim fontHeight AS Single = printFont.GetHeight(e.Graphics)

Ron Allen
 
Hi Ron

I am getting the two pages to print. However the pages are overlapping on
the same page that is two pages on one page. Is there something basic that
I am missing in the print routine. What I am not sure of is how does it
know to start to print on the second page, such as the page break idea. I
have the lno variable rest to ) because it is going to have to start back at
0 for the line position for the second page.

Bill

'-----------------------------------------------------------------

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

'-----------------------------------------------------------------

' Print the Adding Tape

Dim lpp As Integer

Dim lno As Integer

Dim yPos As Single

Dim fontHeight As Single

Dim LeftMargin As Single = e.MarginBounds.Left

Dim TopMargin As Single = e.MarginBounds.Top

Dim printFont As Font

printFont = New Font("Courier New", 10)

fontHeight = printFont.GetHeight(e.Graphics)

lpp = CInt(e.MarginBounds.Height / fontHeight)

lno = 0

NumofLines = lstAddTape.Items.Count

While (count < NumofLines)

yPos = TopMargin + (lno * fontHeight)

e.Graphics.DrawString(lstAddTape.Items(count), printFont, Brushes.Black,
LeftMargin, yPos)

count = count + 1

lno = lno + 1

If (lno > lpp) Then

e.HasMorePages = True

lno = 0

End If

End While

End Sub
 
Bill,
To cause a second page to be printed just set HasMorePages to true and
exit the routine. The PrintDocument's Print routine will then eject the
page and call this routine again to start the next page.
So at the top it is
lno = 0
While (count < NumOfLines) And (lno < lpp) ' print each line that fits on
the page while there is still data
... do some printing
lno = lno + 1
count = count + 1
End While
And then
If (count < NumOfLines) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
at the end of the routine. Then the routine is exited and recalled if you
set HasMorePages to true.

Basically the OnPrintPage routine handler is called once for each page
to be printed so you keep track of the datastream position outside of this
routine, either globally or in a class member variable if you are creating a
complete PrintDocument subclass.
In the event handler routine you set up your page position variables and
then loop through your data printing it as approriate. When the page is
full, if there is more data you set HasMorePages to true otherwise set
HasMorePages to false and then exit the handler. This will cause the
current page to be ejected.

I hope this makes it clearer.

Ron Allen

Bill said:
Hi Ron

I am getting the two pages to print. However the pages are overlapping on
the same page that is two pages on one page. Is there something basic
that I am missing in the print routine. What I am not sure of is how does
it know to start to print on the second page, such as the page break idea.
I have the lno variable rest to ) because it is going to have to start
back at 0 for the line position for the second page.

Bill

'-----------------------------------------------------------------

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

'-----------------------------------------------------------------

' Print the Adding Tape

Dim lpp As Integer

Dim lno As Integer

Dim yPos As Single

Dim fontHeight As Single

Dim LeftMargin As Single = e.MarginBounds.Left

Dim TopMargin As Single = e.MarginBounds.Top

Dim printFont As Font

printFont = New Font("Courier New", 10)

fontHeight = printFont.GetHeight(e.Graphics)

lpp = CInt(e.MarginBounds.Height / fontHeight)

lno = 0

NumofLines = lstAddTape.Items.Count

While (count < NumofLines)

yPos = TopMargin + (lno * fontHeight)

e.Graphics.DrawString(lstAddTape.Items(count), printFont, Brushes.Black,
LeftMargin, yPos)

count = count + 1

lno = lno + 1

If (lno > lpp) Then

e.HasMorePages = True

lno = 0

End If

End While

End Sub
----snip-------
 
Back
Top