HasMorePages is not working

W

William LaMartin

I am having trouble printing a second page of a what should be a two page
document of the contents of a listview. The "e.HasMorePages = True" line
in the code below is executed, but what should be on a new page is printed
over the top 30 lines of the first page. There are 86 lines to print with
linesPerPage = 56.

What is missing to make the printer use a new piece of paper?


Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim linesPerPage As Integer = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Pages = Pages + 1 'originally set to -1
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim line As String = Nothing

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

' Print each item of listview.
Try

While ((count < linesPerPage) And ((count + Pages *
linesPerPage) < Me.ListView1.Items.Count))

line = Me.ListView1.Items(count + Pages *
linesPerPage).ToString
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, New StringFormat)
count += 1
End While

' If more lines exist, print another page.

If ((count + Pages * linesPerPage) < Me.ListView1.Items.Count)
Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

End Sub
 
K

Ken Tucker

Hi,

Why dont you try something like this instead.

Dim iStart as integer = pages * linesperpage
e.HasMorePages = False

Tryfor count = istart to listview1.items.count
If count = istart + linesperpage then
exit for
e.hasmorepages = true
end if

line = Me.ListView1.Items(count).ToString
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, New StringFormat)
Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

Ken
-------------
 
W

William LaMartin

Thanks for the reply. Below is the revised code following your suggestion
(which to me appears to be exactly what is required). However, it produces
the same result: The 36 lines that should appear on a second page appear
printed over the first 36 lines on the first page. A new page is not being
generated.


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

Dim linesPerPage As Integer
Dim count As Integer
Pages = Pages + 1 'originally set to -1
Dim yPos As Single = 0
Dim leftMargin As Single = e.MarginBounds.Left
Dim topMargin As Single = e.MarginBounds.Top
Dim line As String = Nothing
linesPerPage = Int((e.MarginBounds.Height /
printFont.GetHeight(e.Graphics)))
Dim iStart As Integer = Pages * linesPerPage
e.HasMorePages = False
Try

For count = iStart To ListView1.Items.Count - 1
If count = iStart + linesPerPage Then
e.HasMorePages = True
Exit For
End If
line = Me.ListView1.Items(count).Text
yPos = topMargin + (count - iStart) *
printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, New StringFormat)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


End Sub
 
K

Ken Tucker

Hi,

Add a printpreviewdialog to your form. Try showing the document in
the printpreviewdialog. Maybe the problem is with the printer.

Ken
--------------------
 
W

William LaMartin

I solved the problem by giving up on using the PrintDocument control and
just created a PrintDocument object via code. Everything works fine now
except that I am getting an additional blank page at the end of the print
job.

Using the PrintDocument control--with the same code--I could never get it to
start a new page. Perhaps there is a bug in the control. I have Visual
Studio 2003 and version 1.1.4322 of the dot net framework.
 
M

Michael Gray

Try making the line that reads:
If count = iStart + linesPerPage Then
instead
If count >= (iStart + linesPerPage - 1) Then
 

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