【问题标题】:VB.net Printing text from a multiline textbox and several datagridviews after thatVB.net 从多行文本框和之后的几个 datagridviews 打印文本
【发布时间】:2016-01-15 02:48:50
【问题描述】:

之后我试图打印一个多行文本框和几个不同的数据网格视图。我希望这里的一些好心人能够帮助我。

到目前为止,当 datagridview 只有一页时效果很好,之后文本的最后一页会重复。

像这样:

All Fine for Page 1 Problems afoot for every other page after that

对于所有即将阅读我的代码混乱的人,我非常感谢。

这是我正在使用的代码:

Dim mRow As Integer = 0
Dim newpage As Boolean = True
Dim page As Integer = 0
Dim tablepage As Integer = 0
Dim content1 As String
Dim numChars As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.ShowDialog()

    mRow = 0
    newpage = True
    page = 0
    tablepage = 0
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Dim numLines As Integer

    '1 Page
    'TEXT
    If page = 0 Then

        content1 = TextBox6.Text & vbLf & TextBox7.Text & vbLf & TextBox8.Text

            Dim stringForPage As String
            Dim strFormat As New StringFormat()
            Dim PrintFont As Font
            PrintFont = TextBox6.Font
            Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
            strFormat.Trimming = StringTrimming.Word
            e.Graphics.MeasureString(content1, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            stringForPage = content1.Substring(0, numChars)
            Dim rectDraw1 As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, PrintFont.GetHeight(e.Graphics) * numLines)

            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw1, strFormat)

        If numChars < content1.Length Then
            content1 = content1.Substring(numChars)
            e.HasMorePages = True
            page += 1

        Else
            e.HasMorePages = False
            'End If


            'DATAGRIDVIEW
            If tablepage > 0 Then
            With TableDataGridView
                Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                fmt.LineAlignment = StringAlignment.Center
                fmt.Trimming = StringTrimming.EllipsisCharacter
                Dim y As Single = e.MarginBounds.Top '

                Do While mRow < .RowCount

                    Dim row As DataGridViewRow = .Rows(mRow)
                    Dim x As Single = e.MarginBounds.Left
                    Dim h As Single = 0
                    For Each cell As DataGridViewCell In row.Cells
                        Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                        e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                        If (newpage) Then
                            e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                        Else
                            e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                        End If
                        x += rc.Width
                        h = Math.Max(h, rc.Height)

                    Next
                    newpage = False
                    y += h
                    mRow += 1

                    If y + h > e.MarginBounds.Bottom Then ' 800 Then '

                        e.HasMorePages = True
                        newpage = True
                        mRow -= 1
                        Exit Sub
                    End If
                Loop

            End With

        Else

            'DATAGRIDVIEW < 2 page

            With TableDataGridView
                        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                        fmt.LineAlignment = StringAlignment.Center
                        fmt.Trimming = StringTrimming.EllipsisCharacter
                        Dim y As Single = 20 + rectDraw1.Bottom 'e.MarginBounds.Top '


                        Do While mRow < .RowCount

                            Dim row As DataGridViewRow = .Rows(mRow)
                            Dim x As Single = e.MarginBounds.Left
                            Dim h As Single = 0
                            For Each cell As DataGridViewCell In row.Cells
                                Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                                e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                                If (newpage) Then
                                    e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                                Else
                                    e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                                End If
                                x += rc.Width
                                h = Math.Max(h, rc.Height)

                            Next
                            newpage = False
                            y += h
                            mRow += 1

                            If y + h > e.MarginBounds.Bottom Then '800 Then '

                                e.HasMorePages = True
                                newpage = True
                                mRow -= 1
                                tablepage += 1
                                Exit Sub
                            End If
                        Loop

                    End With
                End If

            End If

        'TEXT
        'More than 1 Page
    ElseIf page > 0 Then

        Dim stringForPage As String
            Dim strFormat As New StringFormat()
            Dim PrintFont As Font
            PrintFont = TextBox6.Font
            Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
            strFormat.Trimming = StringTrimming.Word
            e.Graphics.MeasureString(content1, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            stringForPage = content1.Substring(0, numChars)

            Dim rectDraw1 As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, PrintFont.GetHeight(e.Graphics) * numLines)
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw1, strFormat)

            If numChars < content1.Length Then
                content1 = content1.Substring(numChars)
                e.HasMorePages = True
                page += 1
            Else
            e.HasMorePages = False

            'DATAGRIDVIEW
            If tablepage > 0 Then
                With TableDataGridView
                    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                    fmt.LineAlignment = StringAlignment.Center
                    fmt.Trimming = StringTrimming.EllipsisCharacter
                    Dim y As Single = e.MarginBounds.Top '

                    Do While mRow < .RowCount

                        Dim row As DataGridViewRow = .Rows(mRow)
                        Dim x As Single = e.MarginBounds.Left
                        Dim h As Single = 0
                        For Each cell As DataGridViewCell In row.Cells
                            Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                            e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                            If (newpage) Then
                                e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                            Else
                                e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                            End If
                            x += rc.Width
                            h = Math.Max(h, rc.Height)

                        Next
                        newpage = False
                        y += h
                        mRow += 1

                        If y + h > e.MarginBounds.Bottom Then ' 800 Then '

                            e.HasMorePages = True
                            newpage = True
                            mRow -= 1
                            Exit Sub
                        End If
                    Loop

                End With

            Else

                'DATAGRIDVIEW < 2 page

                With TableDataGridView
                    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                    fmt.LineAlignment = StringAlignment.Center
                    fmt.Trimming = StringTrimming.EllipsisCharacter
                    Dim y As Single = 20 + rectDraw1.Bottom 'e.MarginBounds.Top '


                    Do While mRow < .RowCount

                        Dim row As DataGridViewRow = .Rows(mRow)
                        Dim x As Single = e.MarginBounds.Left
                        Dim h As Single = 0
                        For Each cell As DataGridViewCell In row.Cells
                            Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                            e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                            If (newpage) Then
                                e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                            Else
                                e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                            End If
                            x += rc.Width
                            h = Math.Max(h, rc.Height)

                        Next
                        newpage = False
                        y += h
                        mRow += 1

                        If y + h > e.MarginBounds.Bottom Then '800 Then '

                            e.HasMorePages = True
                            newpage = True
                            mRow -= 1
                            tablepage += 1
                            Exit Sub
                        End If
                    Loop

                End With
            End If
        End If

    End If
End Sub

【问题讨论】:

  • 尝试自己缩小问题的范围,没有人会经历所有这些并为您完成工作。
  • 谢谢 - 我会继续努力的。

标签: vb.net printing datagridview textbox multiline


【解决方案1】:

与其一遍又一遍地编写相同的代码,为什么不做类似的事情

Static Pos AS Integer

For I = Pos To TableDataGridView.Rows.Count

 'Do your graphics work here using TableDataGridView.Rows(i)

  If y + h > e.MarginBounds.Bottom Then
     Pos+=i
     e.hasmorepages = true
     Exit For
  end if
next

【讨论】:

  • 感谢您的建议 - 它肯定会使代码效率更高!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 2013-07-31
  • 1970-01-01
相关资源
最近更新 更多