another "hasmorepages" newbie

kh_mneimneh

Member
Joined
Feb 27, 2006
Messages
7
Programming Experience
Beginner
Hi every one
I am a newbie to vb.net.
I am experimenting with printing example.
I want to print 2 colums, on 2 pages, one on every page. I managed to get a second page, but my problem is that all the printing takes place only on the first page, and the second page appears blank.

can anybody help me please. Thanks in advance.

here is my test code

Protected
Sub ThePrintDocument_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles ThePrintDocument.PrintPage
Dim linesPerPage As Single = 0
Static yPosition As Single = 25
Static count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim bottomMargin As Single = ev.MarginBounds.Bottom
Dim line As String = Nothing
Dim printFont As Font = Me.richTextBox1.Font
Dim myBrush As New SolidBrush(Color.Black)
' Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)


linesPerPage = 20
While count < linesPerPage
Dim j As Integer
For j = 0 To 40
' calculate the next line position based on
' the height of the font according to the printing device
'If count = linesPerPage Then
'yPosition = 25 '+ (printFont.GetHeight(ev.Graphics))
'Else
yPosition = yPosition + (printFont.GetHeight(ev.Graphics))
'End If
' draw the next line in the rich edit control
ev.Graphics.DrawString("Hello", printFont, myBrush, leftMargin, yPosition, New StringFormat())
count += 1
If count > linesPerPage Then
ev.HasMorePages = True
leftMargin = 200
'yPosition = 25
Else
ev.HasMorePages = False
End If
Next j
End While
myBrush.Dispose()
End Sub 'ThePrintDocument_PrintPage
 
Why not try with something simpler first? Try to write "page 1" on the first page and "page 2" on the second page. Setting this up in the PrintPage event handler should give you the clue of how this must work logically. This should also lead you to understand how to use an index when working with more complex printing.
 
Thanks JohnH,
But I still do not get it.
Do you have an example code for me, or better yet, can you change my code to serve my need.
Thanks again.
 
I don't quite get what you say you want to print.
If your print is static, you can index and keep track of page numbers only.
simple psuedo code:
VB.NET:
dim page as short
 
sub callprint()
  page=1
  printdocument.print
end sub
 
sub printpage() handles printdoc.printpage
  select case page
  case 1
    'print section 1
  case 2
    'print section 2
  end select
  page+=1
  e.hasmorepages = (page<3)
end sub
If you have more dynamic content that lengthen more than one page, then you can index the lines/records etc. Say your list has count 100, and you have measured room for 40 each page (of course you can measure on the fly too, just checking that there is more room on the page to print one more line from where your position is currently at).
simple psuedo code:
VB.NET:
dim currentline as short
 
sub callprint()
  currentline=0
  printdocument.print
end sub
 
sub printpage() handles printdoc.printpage
  for i as short = currentline to currentline+40
    draw line
    currentline+=1
  next
  e.hasmorepages = (currentline<100)
end sub
You see? First time printpage is executed currentline will go from 0 to 39, next time (page two) it will 40 to 79, last time/page it will 80 to 99. A total of 100 lines ranging from 0 to 99 printed on two and a half pages.
 
Thanks again JohnH,
thanks for your effort and patience.
But it seems that I need something more suitable for dummies.
What I am trying to do is that I am self learning VB.net, I have created a database and I am retrieving data into a dataset, which contents I want to print that might require to be printed on more than one page, and at certain positions, to appear in colums, left justified for text and right justified for numbers. I have managed fine if data fits only on one page, but if more pages.... I am big failure.
this is why I am experimenting with the print example.
I would really be very much obliged if you would alter the code that I posted to print "Hello" at left margin, and "World" at yPosition 200. to be printed in 2 colums, 100 times 40 times per page. Maybe I would then my dilemma reaches its happy ending.
Thank you very much.
 
Back
Top