none
Problem with multiple pages RRS feed

  • Question

  • i want to print a list of names with debits, credits
    the first page prints correctly but the next page prints on the first page and overflows the informations
    I print on a printer and in a pdf and it is same
    I'm wrong I didn't find it
    Can you help me;

     Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim count As Integer                               ' Define database records
            Dim pageCount As Double                            ' Set pages         
            Dim ReportFont As Font = New Font("Arial", 11)
            Dim left1 As Double = SIDE_MARGIN
            Dim left2 As Double
            Dim top1 As Double = TOP_MARGIN
            Dim VertGap As Double = VERTICAL_GAP + 5
            Dim newTop As Double
            Dim xPage, pPage, yPage As Double
            Dim xTotal, pTotal, yTotal As Double
            '--------------------------------------------------------------------------------------------------------------------------
            count = DataGridView1.RowCount
            pageCount = Math.Ceiling(count / LINES_PER_SHEET)
    
            xTotal = 0
            pTotal = 0
            yTotal = 0
            '--------------------------------------------------------------------------------------------------------------------------
            If count > 0 Then
    
                Dim dataIndex As Integer = 0
    
                Dim i As Integer
    
                For i = 0 To pageCount - 1                    ' Number of pages to print                
                    Dim page = i + 1
                    newTop = top1
    
                    '------------ Top ------------ 
    
                    e.Graphics.DrawString(ClubName, ReportFont, Brushes.Black, left1, newTop)
                    CenterPageText("Page : " & page, ReportFont, Brushes.Black, newTop, e)
                    RightPageText(Today, ReportFont, Brushes.Black, newTop, Convert.ToSingle(SIDE_MARGIN) * 2, e)
    
                    newTop = newTop + VertGap
                    e.Graphics.DrawString("Club Name", ReportFont, Brushes.Black, left1, newTop)
    
                    newTop = newTop + VertGap
                    e.Graphics.DrawString("", ReportFont, Brushes.Black, left1, newTop)
    
                    newTop = newTop + VertGap
                    If RadioButton1.Checked Then CenterPageText("Full List", ReportFont, Brushes.Black, newTop, e)
                    If RadioButton2.Checked Then CenterPageText("List 1", ReportFont, Brushes.Black, newTop, e)
                    If RadioButton3.Checked Then CenterPageText("List 2", ReportFont, Brushes.Black, newTop, e)
    
                    newTop = newTop + VertGap
                    e.Graphics.DrawLine(Pens.Black, Convert.ToSingle(left1), Convert.ToSingle(newTop), Convert.ToSingle(PAPER_SIZE_WIDTH), Convert.ToSingle(newTop))
    
                    'newTop = newTop + VertGap
                    e.Graphics.DrawString("No", ReportFont, Brushes.Black, left1, newTop)
                    left2 = left1 + 50
                    e.Graphics.DrawString("Last name", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 150
                    e.Graphics.DrawString("Name", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 150
                    e.Graphics.DrawString("Debit", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 100
                    e.Graphics.DrawString("Credit", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 100
                    e.Graphics.DrawString("Total", ReportFont, Brushes.Black, left2, newTop)
    
                    newTop = newTop + VertGap
                    e.Graphics.DrawLine(Pens.Black, Convert.ToSingle(left1), Convert.ToSingle(newTop), Convert.ToSingle(PAPER_SIZE_WIDTH), Convert.ToSingle(newTop))
    
                    '------------ Main Body ------------ 
                    xPage = 0
                    pPage = 0
                    yPage = 0
    
                    Dim j As Integer
                    For j = 0 To LINES_PER_SHEET - 1
                        If (dataIndex < count) Then
                            If j = 0 Then
                            Else
                                newTop = newTop + VertGap
                            End If
                            e.Graphics.DrawString(DataGridView1.Item(0, dataIndex).Value, ReportFont, Brushes.Black, left1, newTop)
                            left2 = left1 + 50
                            e.Graphics.DrawString(DataGridView1.Item(1, dataIndex).Value, ReportFont, Brushes.Black, left2, newTop)
                            left2 = left2 + 150
                            e.Graphics.DrawString(DataGridView1.Item(2, dataIndex).Value, ReportFont, Brushes.Black, left2, newTop)
                            left2 = left2 + 150
                            xPage = xPage + DataGridView1.Item(3, dataIndex).Value
                            xTotal = xTotal + DataGridView1.Item(3, dataIndex).Value
                            e.Graphics.DrawString(RSet(DataGridView1.Item(3, dataIndex).Value, 11), ReportFont, Brushes.Black, left2, newTop)
                            left2 = left2 + 100
                            pPage = pPage + DataGridView1.Item(4, dataIndex).Value
                            pTotal = pTotal + DataGridView1.Item(4, dataIndex).Value
                            e.Graphics.DrawString(RSet(DataGridView1.Item(4, dataIndex).Value, 12), ReportFont, Brushes.Black, left2, newTop)
                            left2 = left2 + 100
                            yPage = yPage + DataGridView1.Item(5, dataIndex).Value
                            yTotal = yTotal + DataGridView1.Item(5, dataIndex).Value
                            e.Graphics.DrawString(RSet(DataGridView1.Item(5, dataIndex).Value, 14), ReportFont, Brushes.Black, left2, newTop)
                        End If
                        dataIndex += 1
    
                    Next
    
                    '------------ Bottom ------------ 
                    newTop = newTop + VertGap
                    e.Graphics.DrawLine(Pens.Black, Convert.ToSingle(left1), Convert.ToSingle(newTop), Convert.ToSingle(PAPER_SIZE_WIDTH), Convert.ToSingle(newTop))
    
                    'newTop = newTop + VertGap
                    left2 = left1 + 50
                    e.Graphics.DrawString("Page sums: ", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 295
                    e.Graphics.DrawString(RSet(xPage, 11), ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 105
                    e.Graphics.DrawString(RSet(pPage, 12), ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 95
                    e.Graphics.DrawString(RSet(yPage, 14), ReportFont, Brushes.Black, left2, newTop)
    
                    newTop = newTop + VertGap
                    left2 = left1 + 50
                    e.Graphics.DrawString("General sums : ", ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 295
                    e.Graphics.DrawString(RSet(xTotal, 11), ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 105
                    e.Graphics.DrawString(RSet(pTotal, 12), ReportFont, Brushes.Black, left2, newTop)
                    left2 = left2 + 95
                    e.Graphics.DrawString(RSet(yTotal, 14), ReportFont, Brushes.Black, left2, newTop)
    
                    newTop = newTop + VertGap
                    e.Graphics.DrawLine(Pens.Black, Convert.ToSingle(left1), Convert.ToSingle(newTop), Convert.ToSingle(PAPER_SIZE_WIDTH), Convert.ToSingle(newTop))
    
                    ' ------------ Change Page -------------
                    'If newTop >= e.MarginBounds.Bottom Then
                    ' e.HasMorePages = True
                    ' End If
                    If dataIndex <= count Then
                        e.HasMorePages = True
                    End If
                    If dataIndex >= count Then
                        e.HasMorePages = False
                        'Exit Sub
                    End If
                Next
    
            End If
    
        End Sub


    Saturday, December 7, 2019 5:14 PM

Answers

  • at the end of sub, i wrote the follow code

    ' ------------ Change Page -------------
                    'If newTop >= e.MarginBounds.Bottom Then
                    ' e.HasMorePages = True
                    ' End If
                    If dataIndex <= count Then
                        e.HasMorePages = True
                    End If
                    If dataIndex >= count Then
                        e.HasMorePages = False
                        'Exit Sub
                    End If

    But it doesn't change a page and rewrites on the same page

    This is my problem


    Yes I know that is your problem.

    That is why I wrote you an example that works. If you study the example you should see why your code does not work and why.

    I am showing you how to do it correctly in my example.

    You have a for loop in your code for your pages:

                For i = 0 To pageCount - 1

    but you don't need that. That is what is wrong with your code. Do you see a for loop in my example for the page count? No.

    And what did I explain to you in my post? You must EXIT THE PRINTPAGE SUB ROUTINE TO END THE CURRENT PAGE and then set the HasMorePages flag true to RETURN TO THE PRINTPAGE SUB ANE PRINT THE NEXT NEW PAGE.

    What you have using your for loop you don't exit the sub. You just loop and print all the documatne on the same page because you don't exit the sub to end the page and return to start a new page.

    Do it like I showed you. Make my example and study how it works until you understand it. Then change your code to work the same way.

    Make my example and set a debug break point on the start of the sub and then single step through the example one line at a time and watch what happens.

               

    Sunday, December 8, 2019 9:50 AM

All replies

  • To end the current page you exit the printpage sub routine.

    To return to printpage to print a new page you set hasmorepages = true.

    Here is a simplified working example similar to what you have shown.

    Imports System.Drawing.Printing
    
    Public Class Form3
        Private WithEvents PrintDocument1 As New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog
        Private PageCount As Integer
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            Dim pageTotal As Integer = 3
            Dim linesPerPage As Integer = 5
    
            pageCount += 1
    
            e.Graphics.DrawString("Page = " & PageCount.ToString, Font, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
            For i As Integer = 1 To linesPerPage
                e.Graphics.DrawString("Line = " & i.ToString, Font, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y + 30 + (i * 20))
            Next
    
            If PageCount < pageTotal Then
                e.HasMorePages = True
            Else
                PageCount = 0
            End If
    
        End Sub
    End Class

    Saturday, December 7, 2019 9:26 PM
  • at the end of sub, i wrote the follow code

    ' ------------ Change Page -------------
                    'If newTop >= e.MarginBounds.Bottom Then
                    ' e.HasMorePages = True
                    ' End If
                    If dataIndex <= count Then
                        e.HasMorePages = True
                    End If
                    If dataIndex >= count Then
                        e.HasMorePages = False
                        'Exit Sub
                    End If

    But it doesn't change a page and rewrites on the same page

    This is my problem


    Sunday, December 8, 2019 7:23 AM
  • at the end of sub, i wrote the follow code

    ' ------------ Change Page -------------
                    'If newTop >= e.MarginBounds.Bottom Then
                    ' e.HasMorePages = True
                    ' End If
                    If dataIndex <= count Then
                        e.HasMorePages = True
                    End If
                    If dataIndex >= count Then
                        e.HasMorePages = False
                        'Exit Sub
                    End If

    But it doesn't change a page and rewrites on the same page

    This is my problem


    Yes I know that is your problem.

    That is why I wrote you an example that works. If you study the example you should see why your code does not work and why.

    I am showing you how to do it correctly in my example.

    You have a for loop in your code for your pages:

                For i = 0 To pageCount - 1

    but you don't need that. That is what is wrong with your code. Do you see a for loop in my example for the page count? No.

    And what did I explain to you in my post? You must EXIT THE PRINTPAGE SUB ROUTINE TO END THE CURRENT PAGE and then set the HasMorePages flag true to RETURN TO THE PRINTPAGE SUB ANE PRINT THE NEXT NEW PAGE.

    What you have using your for loop you don't exit the sub. You just loop and print all the documatne on the same page because you don't exit the sub to end the page and return to start a new page.

    Do it like I showed you. Make my example and study how it works until you understand it. Then change your code to work the same way.

    Make my example and set a debug break point on the start of the sub and then single step through the example one line at a time and watch what happens.

               

    Sunday, December 8, 2019 9:50 AM
  • thank you very much tommytwotrain


    i deleted the loop and also extracted from the sub some variables that were causing the problem

    the variables each time they printed a new page were zero

    Sunday, December 8, 2019 8:25 PM