473,725 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HLP: e.HasMorePages = True (NOT)

VB.net (standard)

I'm experiencing some difficulties in printing multiple pages using Print
Preview.

The following is my main parts of my printing subs (excess info deleted for
your viewing pleasure). For a single page, the print preview (and an actual
print) are OK. Getting the .HasMorePages doesn't seem to be working for me
here (unless I'm missing something).

I have a Title Block (separate sub)... print my info 'group' (max 10 per
page)... What the following does (and it's at the bottom where I'm screwing
up) is that 10 or Less items is OK. More than 10 items gives me ONE page with
all the data printed over each other (ie: a Page Break does not occur).

I'd appreciate some help here (have spent many hours trying to figure this one
out):

' *************** *************** *************** **************
Private Sub mnuPrinter_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles mnuPrinter.Clic k

... I set up my PageSettings, Fonts, Column Widths, DefaultPageSett ings, etc.
here...

PrintPreviewDia log1.Document = PrintDocument1
PrintPreviewDia log1.ShowDialog ()

End Sub

' *************** *************** *************** **************
Private Sub PrintDocHeader( ByVal e As
System.Drawing. Printing.PrintP ageEventArgs)
.... I set up my Document Header stuff here...
End Sub

' *************** *************** *************** **************
Private Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
System.Drawing. Printing.PrintP ageEventArgs) Handles PrintDocument1. PrintPage

Dim rwCntr As Integer = 0 ' Print Rows Counter (max 16 used)
Dim str As String

' Start Printing Output
For cntrI = 0 To lbWeekItems.Ite ms.Count - 1
rwCntr = rwCntr + 1

' Page Header
If rwCntr = 1 Then PrintDocHeader( e)

.... Now I print my rows (group) of info...
Dim HrTtl As Double
Dim R1 As New RectangleF(X1, Y, W1, 80) ' Col 1 Job
Dim R2 As New RectangleF(X1 + 80, Y, W2, 80) ' Col 2 Job Desc
str = ProjArray(0, cntrI + 1)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R1)
str = ProjArray(1, cntrI + 1)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R2)
..... etc, etc...

.... I Finish off my items with a row (between each group of items)...
Y = Y + 15
' Draw Break Line
With PrintDocument1. DefaultPageSett ings
Y = Y + 20
e.Graphics.Draw Line(Pens.Black , X1, Y, .PaperSize.Heig ht - 50, Y)
End With

' HERE IS WHERE I THINK THE PROBLEM IS???
' =============== =============== ==========
If rwCntr = 10 Then
e.HasMorePages = True
rwCntr = 0
End If

Next ' cntrI

' No More Pages to Print
e.HasMorePages = False

End Sub
Thanks!

Bruce
Jul 21 '05 #1
8 2894
Hi,

The way you wrote your code it always hits e.hasmorepages=
false. Try something like this.

If rwCntr = 10 Then
e.HasMorePages = True
rwCntr = 0
Else
' No More Pages to Print
e.HasMorePages = False
End If

Next ' cntrI
Ken
------------
"Mr. B" <Us**@NoWhere.c om> wrote in message
news:c5******** *************** *********@4ax.c om...
VB.net (standard)

I'm experiencing some difficulties in printing multiple pages using Print
Preview.

The following is my main parts of my printing subs (excess info deleted for your viewing pleasure). For a single page, the print preview (and an actual print) are OK. Getting the .HasMorePages doesn't seem to be working for me here (unless I'm missing something).

I have a Title Block (separate sub)... print my info 'group' (max 10 per
page)... What the following does (and it's at the bottom where I'm screwing up) is that 10 or Less items is OK. More than 10 items gives me ONE page with all the data printed over each other (ie: a Page Break does not occur).

I'd appreciate some help here (have spent many hours trying to figure this one out):

' *************** *************** *************** **************
Private Sub mnuPrinter_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles mnuPrinter.Clic k

... I set up my PageSettings, Fonts, Column Widths, DefaultPageSett ings, etc. here...

PrintPreviewDia log1.Document = PrintDocument1
PrintPreviewDia log1.ShowDialog ()

End Sub

' *************** *************** *************** **************
Private Sub PrintDocHeader( ByVal e As
System.Drawing. Printing.PrintP ageEventArgs)
... I set up my Document Header stuff here...
End Sub

' *************** *************** *************** **************
Private Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
System.Drawing. Printing.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
Dim rwCntr As Integer = 0 ' Print Rows Counter (max 16 used)
Dim str As String

' Start Printing Output
For cntrI = 0 To lbWeekItems.Ite ms.Count - 1
rwCntr = rwCntr + 1

' Page Header
If rwCntr = 1 Then PrintDocHeader( e)

... Now I print my rows (group) of info...
Dim HrTtl As Double
Dim R1 As New RectangleF(X1, Y, W1, 80) ' Col 1 Job
Dim R2 As New RectangleF(X1 + 80, Y, W2, 80) ' Col 2 Job Desc
str = ProjArray(0, cntrI + 1)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R1)
str = ProjArray(1, cntrI + 1)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R2)
.... etc, etc...

... I Finish off my items with a row (between each group of items)...
Y = Y + 15
' Draw Break Line
With PrintDocument1. DefaultPageSett ings
Y = Y + 20
e.Graphics.Draw Line(Pens.Black , X1, Y, .PaperSize.Heig ht - 50, Y)
End With

' HERE IS WHERE I THINK THE PROBLEM IS???
' =============== =============== ==========
If rwCntr = 10 Then
e.HasMorePages = True
rwCntr = 0
End If

Next ' cntrI

' No More Pages to Print
e.HasMorePages = False

End Sub
Thanks!

Bruce

Jul 21 '05 #2
With Deft Fingers, "Ken Tucker [MVP]" <vb***@bellsout h.net> wrote:
The way you wrote your code it always hits e.hasmorepages=
false. Try something like this.

If rwCntr = 10 Then
e.HasMorePages = True
rwCntr = 0
Else
' No More Pages to Print
e.HasMorePages = False
End If

Next ' cntrI


Thanks Ken... but believe it or not... Still doesn't work (this is probably
one of the many variations I've tried. Something is VERY strange here.

This is the only place I enter 'e.HasMorePages '. But my PrintPreview still
shows the two pages overlapping each other.

I got most of my code format from a sample app... that worked fine (only they
were using a WHILE statement and I'm using a FOR).

I don't know... I'm pretty tired of this :(

Thanks anyways (this is all rather depressing)

Bruce
Jul 21 '05 #3
"Mr. B" <Us**@NoWhere.c om> wrote...
I'd appreciate some help here (have spent many hours trying to figure this one out):
I think I can solve the problem but if you'll bear with me for a moment I'd
like to offer an "angle" on solving problems. It might have worked in the
particular case but you might be surprised how "simplifyin g" can lead to the
solution. Reduce the problem down to the fewest number of lines of code...
save the old code of course but eliminate the rectangles, one of the two
drawstring methods, the linebreak and such.

Not specifically because it causes the problem (but it might) but rather
just to get the unrelated things off the table.

Ok... so I think you've misinterpreted how the PrintPage() method works. It
does basically two things... draws the current page and tells the
PrintPreviewDia log if it needs to be called again (for another page.) If
you set .HasMorePages to True it will be called again when the current page
is finished, if you set it to False it will not be called again... even if
there is more data.

You are printing all the data on one page because there is only one call to
PrintPage. It is the repeated calls that will cause the page breaks. The
printing is on top of the previous page simply because you reset the Y
position.

You have to move the following outside of PrintPage...
For cntrI = 0 To lbWeekItems.Ite ms.Count - 1


so it doesn't start the data over each time that PrintPage is called.

Tom
Jul 21 '05 #4
With Deft Fingers, "Tom Leylan" <ge*@iamtiredof spam.com> wrote:
solution. Reduce the problem down to the fewest number of lines of code...
save the old code of course but eliminate the rectangles, one of the two
drawstring methods, the linebreak and such.
Okay... I'll try that. But like I said, it does print out okay (even though
on top of each other). And I took the code from an example CD that works
fine!
Ok... so I think you've misinterpreted how the PrintPage() method works. It
does basically two things... draws the current page and tells the
No kidding (: My first try at VB.net printing !!! (gimme VB6 <grin>)
PrintPreviewDi alog if it needs to be called again (for another page.) If
you set .HasMorePages to True it will be called again when the current page
is finished, if you set it to False it will not be called again... even if
Hmmm... if you compare my original post stuff vs. this (from the example), I
can't see where it differs in the 2nd page call vs. mine!!!

(part of the Private Sub PrintDocument1_ PrintPage sub...)

While itm < ListView1.Items .Count
Dim str As String
str = ListView1.Items (itm).Text
e.Graphics.Draw String(str, tableFont, Brushes.Black, X1, Y)
str = ListView1.Items (itm).SubItems( 1).Text
Dim R As New RectangleF(X2, Y, W2, 80)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R)
Dim lines, cols As Integer
e.Graphics.Meas ureString(str, tableFont, New SizeF(W2, 50), New
StringFormat(), cols, lines)
Dim subitm As Integer, Yc As Integer
Yc = Y
For subitm = 2 To ListView1.Items (itm).SubItems. Count - 1
str = ListView1.Items (itm).SubItems( subitm).Text
e.Graphics.Draw String(str, tableFont, Brushes.Black, X3, Yc)
Yc = Yc + tableFont.Heigh t + 2
Next
Y = Y + lines * tableFont.Heigh t + 5
Y = Math.Max(Y, Yc)
With PrintDocument1. DefaultPageSett ings
e.Graphics.Draw Line(Pens.Black , .Margins.Left, Y, .PaperSize.Widt h -
..Margins.Right , Y)
If Y > 0.95 * (.PaperSize.Hei ght - .Margins.Bottom ) Then
e.HasMorePages = True
Exit Sub
End If
End With
itm = itm + 1
End While
e.HasMorePages = False

You have to move the following outside of PrintPage...
For cntrI = 0 To lbWeekItems.Ite ms.Count - 1

so it doesn't start the data over each time that PrintPage is called.


Thanks Tom... I'll examine my example again in detail and see just where I'm
going wrong in the logic (as usual, it usually IS just something damn stupid
that you can't see).

Thanks again! I'll keep on trying...

Bruce
Jul 21 '05 #5
"JamesBV" <No****@telus.n et> wrote...
With Deft Fingers, "Tom Leylan" <ge*@iamtiredof spam.com> wrote:
Reduce the problem down to the fewest number of lines of code.
Okay... I'll try that. But like I said, it does print out okay (even though on top of each other). And I took the code from an example CD that works
fine!
It's a just a rule to keep in mind. With less code you have fewer potential
causes and fewer things to consider when looking for a solution. I can
guarantee it isn't drawing the linebreak but why make your eyes skip over
that code (and the rest) for "hours."
Hmmm... if you compare my original post stuff vs. this (from the example), I can't see where it differs in the 2nd page call vs. mine!!!
If the following is the code a careful reading will reveal the differences.

this one loops while there is still data While itm < ListView1.Items .Count
Yours resets the counter every time it prints a page For cntrI = 0 To lbWeekItems.Ite ms.Count - 1
this one exits if the end of the page is reached If Y > 0.95 * (.PaperSize.Hei ght - .Margins.Bottom ) Then
e.HasMorePages = True
Exit Sub
Yours doesn't Next ' cntrI
This one tells it to stop printing pages when it runs out of data End While
e.HasMorePages = False
Yours tells it to stop printing pages when it runs out of data
except you printed all your data in your for... next loop ' No More Pages to Print
e.HasMorePages = False Thanks Tom... I'll examine my example again in detail and see just where I'm going wrong in the logic (as usual, it usually IS just something damn stupid that you can't see).


It sounds like just a little misunderstandin g it how it works. PrintPage
doesn't render a report, just a page. You can see why if you print a very
long report. You will be able to interrupt the report while the pages are
printing... yet you won't have any code that says it should be
interruptible. It gets that chance as each page is rendered and it goes
back around for another page.

So basically you have to write the code such that PrintPage can generate a
page of printing (moving a record pointer or incrementing an index or
whatever to change the data) as well as determine if it has run out of data
(that's when it returns False) but it can't initialize all the control
variables or they will get reset each time it goes to print a page.

My suggestion would be you pull all the non-essentials out... just print
"Page " & pagenum.tostrin g & " line " & linenum.tostrin g on (for instance 3
pages with 10 lines on each page.) Once you have the simple example
operating you can go back to finishing your more elaborate one.

Tom


Jul 21 '05 #6
It's a just a rule to keep in mind. With less code you have fewer potential
causes and fewer things to consider when looking for a solution. I can


GOT IT! (sheeeesshhhh)

Apart from a couple of things that needed fixing, I finally 'saw' the problem.

As you suggested, the Next Page wasn't been sent. And where the problem was,
was I wasn't exiting the sub (like my sample did). What I had to do was set
up my print sub using the WHILE statement. Then I added the EXIT SUB at the
correct spot... Voila'.

' That's it for this page...
If rwCntr = 18 Then
e.HasMorePages = True
cntrI = cntrI + 1
rwCntr = 0
Exit Sub < -------- HERE !!!!
End If

cntrI = cntrI + 1
End While

' No More Pages to Print
e.HasMorePages = False
What I didn't realize was that (and correct me if I'm wrong) it appears that
the PrintDocument1_ PrintPage sub has to be EXITed for each Page. That is what
it looks like to me. That being so, no wonder why I didn't see it as I'm
still in VB6 mode (:

Anyways.... finally I'm moving on to the next problem.

Thanks again for the 'tips'.

James
Jul 21 '05 #7
"JamesBV" <No****@telus.n et> wrote...
' That's it for this page...
If rwCntr = 18 Then
e.HasMorePages = True
cntrI = cntrI + 1
rwCntr = 0
Exit Sub < -------- HERE !!!!
End If
I don't have the exact code but generally speaking (if the phrase isn't
bothering you too much) e.HasMorePages isn't true simply because rwCntr =
18. You might have other logic that prevents it (I'm just guessing there
isn't) but your counter can hit 18 exactly at the point there isn't any more
data.
What I didn't realize was that (and correct me if I'm wrong) it appears that the PrintDocument1_ PrintPage sub has to be EXITed for each Page.
Well that's what I was trying to say. I'd phrase it differently though. It
has to be _called_ for each page. It's a "print page event" so you don't
print a report in it unless your report happens to be only one page.
Anyways.... finally I'm moving on to the next problem.
Thanks again for the 'tips'.


Sounds good...
Jul 21 '05 #8
Anyways.... finally I'm moving on to the next problem.
Thanks again for the 'tips'.


Sounds good...


Yeah... too good to be true. NOW... somehow I'm printing blank sheets!!!! No
kidding. Just tried it on the printer (and to a PDF file)... nothing!

I think I'll go buy a MAC now thank you! :(

I think I'm going to leave this until tomorrow. I (for the life of me) can't
see what I've done now. I've tried my old (broken) code... I get the
overlapping... but it does print!

My new code does show the Print preview's with multiple pages... but nothing
comes out to the printer (unless my black toner is white?) (:

This is just too much for me...

Jame
Jul 21 '05 #9

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

Similar topics

3
8356
by: DGeorge | last post by:
I have a problem where after setting the HasMorePages = True will not print the 2nd Pages. Does anybody have an idea or knows this problem. Thanks for any advice
8
9880
by: Mr. B | last post by:
VB.net (standard) I'm experiencing some difficulties in printing multiple pages using Print Preview. The following is my main parts of my printing subs (excess info deleted for your viewing pleasure). For a single page, the print preview (and an actual print) are OK. Getting the .HasMorePages doesn't seem to be working for me here (unless I'm missing something).
1
1478
by: Kyle Walz | last post by:
I'm working on an app where each page is a like a form (not Windows form) with a set format. Using MSVB.NET 2003 with 1.1 framework. Here's some code: Private Sub PrintText(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Dim brush As Brush = New SolidBrush(Color.Black) Dim font As Font = New Font("Arial", 10, FontStyle.Regular) ' first page
2
7906
by: Leo | last post by:
I set HasMorePages to true, but it doesn't let me jump to next page. (Of course, if I remove the HasMorePages = false in my if statement, it will create hundreds of pages). Any suggestion? Thanks in advance. Leo My code to call the print event: .... {
1
11190
by: kig25 | last post by:
Hello, When using the VB.NET PrintDocument class, I seem to be encountering an issue where the sub pd_PrintPage handles PrintDocument.PrintPage (upon continuing if HasMorePages = true) will paint the next page on top of the original page. In a sample data case where the source is long enough to fill three pages, I end up with four calls to pd_PrintPage which render onto two printed sheets. Historical posts in this forum from 2003 and...
6
4606
by: moti | last post by:
Whenever I use PrintDocument.Print() to print a page it goes to PrintDocument_PrintPage and stays there forever unless I set e.HasMorePages to False. When I set it to False it prints the page but is closes the print file. The effect is that it prints all my pages but creates a print job for each page. So if I print 100 pages I get 100 print jobs. The only way I went around this was to put all my page-filling code within...
0
1565
by: creedence | last post by:
Hello I have a problem with printing data from dataset.I dont know how to make that print is go on to the next page. I know that is something with hasmorepages = true but i dont Know how to use it. Here is my code, so every help is greatful. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
0
1852
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview sith data Code below works fine if I print the data if there is only 1 page (without using '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If intRowPos <= 10 Then e.HasMorePages = False
2
2930
by: GGSoft | last post by:
Please Can't understand how to use HasMorepages. I spent a lot of time on this task. but I have no result. For examle I want to print every 45 record on each new page. Please Rewrite the following code in correct manner. I dont know where to place HasmorePages=true and HasmorePages=false Private Sub Pr_PrintReg(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pr.PrintPage ...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.