首页 > 解决方案 > 如果当前页面已满,如何在下一个空白页中打印循环结果

问题描述

我有以下代码来打印凭证,但是如果页面已满,则在打印 SQL 查询结果时遇到循环部分的问题,它会使用同一页面重新打印。 在此处输入图像描述

Private Sub ATATprint_PrintPage(sender As Object, e As PrintPageEventArgs) Handles ATATprint.PrintPage

    Dim Brush1 As New SolidBrush(Color.Black)
    Dim ValueBrush As New SolidBrush(Color.DarkGreen)
    Dim lblFont As Font = New Font("B Yekan", 10, FontStyle.Regular)
    Dim ValueFont As Font = New Font("Agency FB", 10, FontStyle.Bold)
    Dim ypos As Integer = 300
    Dim pn As Integer = 1

    Dim str(6) As String
    str(0) = TrnAccountType
    str(1) = TrnAccountNo
    str(2) = TrnAccountName
    str(3) = TrnCurrecy
    str(4) = TrnExRate
    str(5) = TrnAmount
    str(6) = TrnNarration
    Try
        Dim adapter As New SqlDataAdapter("select case when trd_DrCr = 'Dr' then 'Debit' else 'Credit' end, 
                                            isnull(acc_Ccy, '')+'-'+Convert(nvarchar,trd_Account), acc_Name, trd_ccy, format(trd_ExRate,'#,###,###.0000'), format(trd_Amount, '#,###,###.00'), trd_Narration
                                            from TransactionDetails join Accounts on Accounts.acc_Number = TransactionDetails.trd_Account where trd_TrnRef = '" & fncTrnReference.Text & "'", connection)
        Dim table As New DataTable
        adapter.Fill(table)
        For row As Integer = 0 To table.Rows.Count - 1
            For col As Integer = 0 To table.Columns.Count - 1
                e.Graphics.DrawString(str(col), lblFont, Brush1, 100, ypos)
                e.Graphics.DrawString(table.Rows(row)(col).ToString, ValueFont, ValueBrush, 200, ypos)
                ypos += 15
            Next
            ypos += 30
            If ypos > 900 Then
                ypos = 200
                e.HasMorePages = True
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    e.HasMorePages = False
End Sub

标签: vb.net

解决方案


您需要在 PrintPage 方法之外填充适配器,并且还需要跟踪 for 循环中行变量的值。再次调用 PrintPage 方法时,从该点开始您的 For 循环。

此外,如果您使用标准颜色,则无需创建新的 SolidColorBrushes。

最后,一定要在完成后处理你的字体。我在下面代码的 finally 块中做到了这一点。

此代码未经测试,因此可能需要进行一些更改以满足您的需求,但它应该会给您一些想法。

'These variables are declared outside the PrintPage method so they retain their values between calls.
Private adapter As SqlDataAdapter = Nothing
Private savedRowValue As Integer = 0
Private table As DataTable = Nothing

Private Sub ATATprint_PrintPage(sender As Object, e As PrintPageEventArgs) Handles ATATprint.PrintPage

    Dim Brush1 As Brushes.Black       'Use the standard brushes here.
    Dim ValueBrush Brushes.DarkGreen
    Dim lblFont As Font = New Font("B Yekan", 10, FontStyle.Regular)
    Dim ValueFont As Font = New Font("Agency FB", 10, FontStyle.Bold)
    Dim ypos As Integer = 300
    Dim pn As Integer = 1

    Dim str(6) As String
    str(0) = TrnAccountType
    str(1) = TrnAccountNo
    str(2) = TrnAccountName
    str(3) = TrnCurrecy
    str(4) = TrnExRate
    str(5) = TrnAmount
    str(6) = TrnNarration

    Try

        'Create the data adapter and fill the DataTable only on the first time the PrintPage method is called
        If adapter Is Nothing Then
            adapter As New SqlDataAdapter("select case when trd_DrCr = 'Dr' then 'Debit' else 'Credit' end, 
                                            isnull(acc_Ccy, '')+'-'+Convert(nvarchar,trd_Account), acc_Name, trd_ccy, format(trd_ExRate,'#,###,###.0000'), format(trd_Amount, '#,###,###.00'), trd_Narration
                                            from TransactionDetails join Accounts on Accounts.acc_Number = TransactionDetails.trd_Account where trd_TrnRef = '" & fncTrnReference.Text & "'", connection)
            table = New DataTable
            adapter.Fill(table)
        End If

        'Start the For loop at the saved value instead of at 0 
        For row As Integer = savedRowValue To table.Rows.Count - 1
            For col As Integer = 0 To table.Columns.Count - 1
                e.Graphics.DrawString(str(col), lblFont, Brush1, 100, ypos)
                e.Graphics.DrawString(table.Rows(row)(col).ToString, ValueFont, ValueBrush, 200, ypos)
                ypos += 15
            Next
            ypos += 30
            If ypos > 900 Then
                ypos = 200
                e.HasMorePages = True
                savedRowValue = row + 1    'Save the value of the current row
                Exit Sub     
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        lblFont.Dispose()      'Dispose of your fonts here
        ValueFont.Dispose()
    End Try

    e.HasMorePages = False
End Sub

推荐阅读