第一次打印工作,但第二次打印空白页

时间:2018-06-15 17:45:00

标签: vb.net winforms visual-studio-2012

我有一个我建的Windows应用程序有5个标签。在2个选项卡上,我只提供了在datagridview.rowcount大于0时打印的选项。这将允许显示打印按钮。我可以很好地打印第一个报告,但如果我选择不同的参数并重新填充datagridview或移动到另一个选项卡并填充其datagridview并尝试打印我只在每次第一次打印作业后获得空白页。有什么想法为什么?

这是所有代码

    Dim strFormat As New StringFormat()
    Dim arrColumnLefts, arrColumnWidths As New ArrayList()
    Dim iCellHeight, iTotalWidth, iCount, iLeftMargin, iTopMargin, iTmpWidth, iHeaderHeight, iRow As Integer
    Dim bFirstPage, bNewPage, bMorePagesToPrint As Boolean
    Dim dgvGridCol, GridCol As DataGridViewColumn
    Dim GridRow As DataGridViewRow

    Public Sub btn_print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_print.Click

        If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DocumentName = "Report Printing"
            PrintDocument1.Print()
        End If
    End Sub

    'Handles the begin print event of print document
    Public Sub printDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        Try
            strFormat = New StringFormat()
            strFormat.Alignment = StringAlignment.Near
            strFormat.LineAlignment = StringAlignment.Center
            strFormat.Trimming = StringTrimming.EllipsisCharacter
            arrColumnLefts.Clear()
            arrColumnWidths.Clear()
            iCellHeight = 0
            iCount = 0
            bFirstPage = True
            bNewPage = True
            iTotalWidth = 0
            If TabControl1.SelectedTab.Text = "Reports" Then
                For Each dgvGridCol As DataGridViewColumn In DataGridView4.Columns
                    iTotalWidth += dgvGridCol.Width
                Next
            ElseIf TabControl1.SelectedTab.Text = "Stats" Then
                For Each dgvGridCol As DataGridViewColumn In DataGridView1.Columns
                    iTotalWidth += dgvGridCol.Width
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub


    'Handles the print page event of print document
    Public Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        If TabControl1.SelectedTab.Text = "Reports" Then
            Try
                'Set the left margin
                Dim iLeftMargin As Integer = e.MarginBounds.Left
                'Set the top margin
                Dim iTopMargin As Integer = e.MarginBounds.Top
                'Whether more pages have to print
                Dim bMorePagesToPrint As Boolean = False
                Dim iTmpWidth As Integer = 0
                'For the first page to print, set the cell width and header height 
                If bFirstPage Then
                    For Each GridCol As DataGridViewColumn In DataGridView4.Columns
                        iTmpWidth = CInt((Math.Floor(CDbl((CDbl(GridCol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * (CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))))
                        iHeaderHeight = CInt((e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height)) + 11
                        arrColumnLefts.Add(iLeftMargin)
                        arrColumnWidths.Add(iTmpWidth)
                        iLeftMargin += iTmpWidth
                    Next
                End If
                'Loop until all the grid rows get printed
                While iRow <= DataGridView4.Rows.Count - 1
                    Dim GridRow As DataGridViewRow = DataGridView4.Rows(iRow)
                    'Set the cell height
                    iCellHeight = GridRow.Height + 5
                    Dim iCount As Integer = 0
                    'Check whether the current page settings allows more rows to print
                    If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
                        bNewPage = True
                        bFirstPage = False
                        bMorePagesToPrint = True
                        Exit While
                    Else
                        If bNewPage Then
                            'Draw header
                            e.Graphics.DrawString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView4.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView4.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            If CheckedListBox5.SelectedItem = "Multi" Then
                                Dim strDateMulti As String = report_start.Value.ToLongDateString() & "  -  " + report_end.Value.Date.ToLongDateString()
                                'Draw Date For Multi-Day Report
                                e.Graphics.DrawString(strDateMulti, New Font(DataGridView4.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDateMulti, New Font(DataGridView4.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(New Font(DataGridView4.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            ElseIf CheckedListBox5.SelectedItem = "Daily" Then
                                Dim strDateDaily As String = report_start.Value.ToLongDateString()
                                'Draw Date For Daily Report
                                e.Graphics.DrawString(strDateDaily, New Font(DataGridView4.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDateDaily, New Font(DataGridView4.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(New Font(DataGridView4.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            End If
                            'Draw Columns
                            iTopMargin = e.MarginBounds.Top
                            For Each GridCol As DataGridViewColumn In DataGridView4.Columns
                                e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
                                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight), strFormat)
                                iCount += 1
                            Next
                            bNewPage = False
                            iTopMargin += iHeaderHeight
                        End If
                        iCount = 0
                        'Draw Column contents
                        For Each Cel As DataGridViewCell In GridRow.Cells
                            If Cel.Value IsNot Nothing Then
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), CSng(iTopMargin), CInt(arrColumnWidths(iCount)), CSng(iCellHeight)), strFormat)
                            End If
                            'Drawing Cell Borders
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iCellHeight))
                            iCount += 1
                        Next
                    End If
                    iRow += 1
                    iTopMargin += iCellHeight
                End While
                'If more lines exist, print another page
                If bMorePagesToPrint Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
            Catch exc As Exception
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
            'PRINT STATS TAB
        ElseIf TabControl1.SelectedTab.Text = "Stats" Then
            Try
                'Set the left margin
                Dim iLeftMargin As Integer = e.MarginBounds.Left
                'Set the top margin
                Dim iTopMargin As Integer = e.MarginBounds.Top
                'Whether more pages have to print
                Dim bMorePagesToPrint As Boolean = False
                Dim iTmpWidth As Integer = 0
                'For the first page to print, set the cell width and header height 
                If bFirstPage Then
                    For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                        iTmpWidth = CInt((Math.Floor(CDbl((CDbl(GridCol.Width) / CDbl(iTotalWidth) * CDbl(iTotalWidth) * (CDbl(e.MarginBounds.Width) / CDbl(iTotalWidth)))))))
                        iHeaderHeight = CInt((e.Graphics.MeasureString(GridCol.HeaderText, GridCol.InheritedStyle.Font, iTmpWidth).Height)) + 11
                        arrColumnLefts.Add(iLeftMargin)
                        arrColumnWidths.Add(iTmpWidth)
                        iLeftMargin += iTmpWidth
                    Next
                End If
                'Loop until all the grid rows get printed
                While iRow <= DataGridView1.Rows.Count - 1
                    Dim GridRow As DataGridViewRow = DataGridView1.Rows(iRow)
                    'Set the cell height
                    iCellHeight = GridRow.Height + 5
                    Dim iCount As Integer = 0
                    'Check whether the current page settings allows more rows to print
                    If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
                        bNewPage = True
                        bFirstPage = False
                        bMorePagesToPrint = True
                        Exit While
                    Else
                        If bNewPage Then
                            If CheckedListBox3.SelectedItem = "All" And CheckedListBox2.SelectedItem = "Department" Then
                                'Draw header for ALL-Department
                                e.Graphics.DrawString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            ElseIf CheckedListBox3.SelectedItem = "All" And CheckedListBox2.SelectedItem = "AssociateID" Then
                                'Draw header for ALL -ID
                                e.Graphics.DrawString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            ElseIf CheckedListBox3.SelectedItem = "Sum" And CheckedListBox2.SelectedItem = "AssociateID" Then
                                'Draw header SUM -ID
                                e.Graphics.DrawString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
                            ElseIf CheckedListBox3.SelectedItem = "Sum" And CheckedListBox2.SelectedItem = "Department" Then
                                'Draw header SUM -Dept
                                e.Graphics.DrawString(UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

                            End If
                            'Draw Date
                            Dim strDate As String = start_stat.Value.ToLongDateString() & "  -  " + end_stat.Value.Date.ToLongDateString()
                            e.Graphics.DrawString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("(" + UCase(txt_ID.Text).ToString() & ") " + UCase(txt_DeptCode.Text).ToString() & "  -  " + UCase(txt_OpCode.Text).ToString(), New Font(New Font(DataGridView1.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)


                            'Draw Columns
                            iTopMargin = e.MarginBounds.Top
                            For Each GridCol As DataGridViewColumn In DataGridView1.Columns
                                e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
                                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight))
                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iHeaderHeight), strFormat)
                                iCount += 1
                            Next
                            bNewPage = False
                            iTopMargin += iHeaderHeight
                        End If
                        iCount = 0
                        'Draw Column contents
                        For Each Cel As DataGridViewCell In GridRow.Cells
                            If Cel.Value IsNot Nothing Then
                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CInt(arrColumnLefts(iCount)), CSng(iTopMargin), CInt(arrColumnWidths(iCount)), CSng(iCellHeight)), strFormat)
                            End If
                            'Drawing Cell Borders
                            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLefts(iCount)), iTopMargin, CInt(arrColumnWidths(iCount)), iCellHeight))
                            iCount += 1
                        Next
                    End If
                    iRow += 1
                    iTopMargin += iCellHeight
                End While
                'If more lines exist, print another page
                If bMorePagesToPrint Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
            Catch exc As Exception
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try

        End If

    End Sub

0 个答案:

没有答案