简体   繁体   中英

VB.net HasMorePages Loop

so, I am trying to make a programme that print pages from an access database I Created evrything and evrything was working fine, than I found out that I would need to print more than one page , after researching I foung the HasMorePages loop, I went thought it and tried many tutorials, but it didn't goes well what I did is this

  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim Fontt As Font = New Drawing.Font("Time New Roman", 10)
    Dim Font3 As Font = New Drawing.Font("Time New Roman", 12, FontStyle.Bold)
    Dim Typp As Font = New Drawing.Font("Time New Roman", 30)
    Dim Cred As Font = New Drawing.Font("Time New Roman", 20)
    Dim Font2 As Font = New Drawing.Font("Time New Roman", 10, FontStyle.Bold)


    If NumOrdre.Text = "Numero" Then
        R = 170
        TC = Nothing
        TA = Nothing
        C = Nothing
        x = Nothing
        L = Nothing
        D = Nothing
        F = Nothing
        MT = Nothing
        Q = Nothing
        PR = Nothing
        M = Nothing
        NC = Nothing
        Dim p As Integer
        For i = 0 To ds.Tables(2).Rows.Count - 1
            If Val(ComboBox1.Text) = Val(ds.Tables(2).Rows(i)("ID")) Then
                p = i
                Exit For
            End If
        Next
        x = ds.Tables(2).Rows(p)("Client")
        T = ds.Tables(2).Rows(p)("Datee")
        NC = ds.Tables(2).Rows(p)("NumDoc")


        F = "Bon De Livraison"


        For Each row As DataRow In ds.Tables(1).Rows

            If (R > e.MarginBounds.Bottom - 30) Then
                R = 20
                e.HasMorePages = True

                Exit Sub
            Else
                e.HasMorePages = False
            End If










            If row.Item("NumDoc") = ds.Tables(2).Rows(p)("NumDoc") And row.Item("ClientFourni") = ds.Tables(2).Rows(p)("Client") Then
                R = R + 30
                L = row.Item("Ref")
                Dim p2 As Integer
                For i2 = 0 To ds.Tables("Articles").Rows.Count - 1
                    If L = ds.Tables("Articles").Rows(i2)("RefPerso") Then
                        p2 = i2
                        Exit For
                    End If
                Next

                D = ds.Tables("Articles").Rows(p2)("Dessi")

                Dim p3 As Integer
                For i3 = 0 To ds.Tables(1).Rows.Count - 1
                    If L = ds.Tables(1).Rows(i3)("Ref") And ds.Tables(1).Rows(i3)("NumDoc") = NC And ds.Tables(1).Rows(i3)("ClientFourni") = ds.Tables(2).Rows(p)("Client") Then
                        p3 = i3
                        Exit For
                    End If
                Next
                Dim p4 As Integer
                For i4 = 0 To ds.Tables("Clients").Rows.Count - 1
                    If x = ds.Tables("Clients").Rows(i4)("Nom") Then
                        p4 = i4
                        Exit For
                    End If
                Next
                C = ds.Tables("Clients").Rows(p4)("Credit")


                Q = ds.Tables(1).Rows(p3)("Quant")
                PR = ds.Tables(1).Rows(p3)("PrixVente")

                M = PR * Q
                MT = MT + (PR * Q)

                e.Graphics.DrawString(Q, Fontt, Brushes.Black, 581, R + 5)
                e.Graphics.DrawString(D, Fontt, Brushes.Black, 140, R + 5)
                e.Graphics.DrawString(L, Fontt, Brushes.Black, 32, R + 5)
                e.Graphics.DrawString(PR, Fontt, Brushes.Black, 521, R + 5)
                e.Graphics.DrawString(M, Fontt, Brushes.Black, 650, R + 5)

                e.Graphics.DrawRectangle(Pens.Black, 130, R, 390, 30)
                e.Graphics.DrawRectangle(Pens.Black, 31, R, 99, 30)
                e.Graphics.DrawRectangle(Pens.Black, 520, R, 60, 30)
                e.Graphics.DrawRectangle(Pens.Black, 580, R, 60, 30)
                e.Graphics.DrawRectangle(Pens.Black, 640, R, 100, 30)

            End If


        Next row

        e.Graphics.DrawString("Reférence", Font3, Brushes.Black, 32, 175)
        e.Graphics.DrawRectangle(Pens.Black, 31, 170, 99, 30)
        e.Graphics.DrawString("Total", Font3, Brushes.Black, 650, 175)
        e.Graphics.DrawRectangle(Pens.Black, 640, 170, 100, 30)
        e.Graphics.DrawString("Quant", Font3, Brushes.Black, 581, 175)
        e.Graphics.DrawRectangle(Pens.Black, 580, 170, 60, 30)
        e.Graphics.DrawString("Total", Font3, Brushes.Black, 550, R + 35)
        e.Graphics.DrawRectangle(Pens.Black, 520, R + 30, 120, 30)
        e.Graphics.DrawString(MT, Fontt, Brushes.Black, 650, R + 35)
        e.Graphics.DrawRectangle(Pens.Black, 640, R + 30, 100, 30)
        e.Graphics.DrawString(F, Typp, Brushes.Black, 280, 10)
        e.Graphics.DrawString("Client :", Font3, Brushes.Black, 130, 75)
        e.Graphics.DrawString("Numéro:", Font3, Brushes.Black, 130, 125)
        e.Graphics.DrawString(NC, Fontt, Brushes.Black, 220, 125)
        e.Graphics.DrawString("Date:", Font3, Brushes.Black, 450, 125)
        e.Graphics.DrawString(T, Fontt, Brushes.Black, 530, 125)
        e.Graphics.DrawString(x, Fontt, Brushes.Black, 220, 75)
        e.Graphics.DrawString("Désignations", Font3, Brushes.Black, 140, 175)
        e.Graphics.DrawRectangle(Pens.Black, 130, 170, 390, 30)
        e.Graphics.DrawString("Prix", Font3, Brushes.Black, 531, 175)
        e.Graphics.DrawRectangle(Pens.Black, 520, 170, 60, 30)
        e.Graphics.DrawRectangle(Pens.Black, 120, 120, 600, 30)
        e.Graphics.DrawRectangle(Pens.Black, 120, 70, 600, 30)
        e.Graphics.DrawString("Crédit :", Font3, Brushes.Black, 450, 75)
        e.Graphics.DrawString(C, Font3, Brushes.Black, 530, 75)

    End If





End Sub

The result I get when I try to print is that the HasMorePages Loop doesn't stop

Setting has more pages will tell vb to run the entire sub again. In your case sense you never change the value of r it always sets it to true.

What I do is use a variable for the y value on all my print statements then increase the value of that variable until I reach the bottom of the page (plus a footer margin) then if you have more data to print set has more pages otherwise end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM