• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1149
  • Last Modified:

Printing mutipul pages VB.NET 2005

Hi

I am current trying to design a printed report for my application. In the subroutine below i am drawing my report. However i need to know how to insert a page break / start a new page. I am populating my page with data from a sql query. once i have reached a certain number of rows i want to be able to start a new page, i have tried adding e.hasmorepages = true after if rn>25 inside my while loop but this seams to generate an infinate number of pages all the same as the first. can anybody please help?


 Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
 rn = 0
 Try
          cnafm.Open()
            Dim notes1 As SqlDataReader = getnotes.ExecuteReader
            With notes1
                If .HasRows Then
                    While .Read
                        If rn > 25 Then
?????e.hasmorepages=true?????
                            rn = 0
                        End If
                        fn = notes1(0).ToString
                        g.DrawString(fn, arial, Brushes.Black, (e.MarginBounds.Left + 2), (e.MarginBounds.Top + 30) + (rn * 40))
                        rn=rn+1
                    End While
                End If
            End With
        Finally
            cnafm.Close()
        End Try
0
dr_dudd
Asked:
dr_dudd
  • 4
1 Solution
 
DhaestCommented:
0
 
NBSO_ISSCommented:
Try kicking out of your while loop when M>25

Then, if there are more pages (more data to print on next page), set e.hasmorepages=true otherwise, e.hasmorepages=false.

It should go through the Print_Page routine again.
0
 
NBSO_ISSCommented:
Actually that may not work because you are calling SQL in your PrintPage statement...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mankowitzCommented:
You have to fire the printpage function for EACH page that you want to print, which means that you have to print 25 lines, exit the sub. Since hasmorepages is true, the sub will be called again and again until hasmorepages is false.
'Globals
dim notes1 as sqldatareader


' Load the db in the beginprint sub
        Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
          cnafm.Open()
          notes1 = getnotes.ExecuteReader
end



 Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
dim  rn as integer = 0
e.hasmorepages=false
            With notes1
                If .HasRows Then
                    While .Read
                        fn = notes1(0).ToString
                        g.DrawString(fn, arial, Brushes.Black, (e.MarginBounds.Left + 2), (e.MarginBounds.Top + 30) + (rn * 40))
                        rn=rn+1
                        If rn > 25 Then
                            e.hasmorepages=true
                            exit sub
                        End If
                    End While
                End If
            End With
end sub
0
 
NBSO_ISSCommented:
Here is a way to do it with a string in a RichTextBox.  If you declare notes1 , rn globally, it should work...

http://www.dotnet4all.com/Dot-Net-Books/2004/08/how-to-print-from-rich-text-box.html
0
 
NBSO_ISSCommented:
Although rn is being reset to 0 each time, so it doesn't need to be global...
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now