0

我在代码中使用 e.HasMorePages

For x As Integer = RowNo To dgv.Rows.Count - 1
    Dim mypen As New Pen(Color.Black, 6)
    e.Graphics.DrawString(dgv.Rows(x - 1).Cells(0).Value.ToString(), f, Brushes.Black, 645, yElementy)

    If RowNo Mod 6 = 0 Then
        RowNo += 1
        e.HasMorePages = True
        Exit For
     End If

     RowNo += 1
Next

如何使用 e.HasMorePages 和代码

For Each myRow In dtn.Rows
    ListView1.Items.Add(n + ListView1.Items.Count + 1)
    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(13).ToString())
    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(5).ToString())
    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(14).ToString())

    'i want to add here
Next
4

2 回答 2

1

典型布局:计数变量(类级别),用于记住我们在项目打印中所处的位置。我喜欢List(Of String)for 循环和打印。在 printPage 事件中,您需要有一个变量,用于将记录打印到哪一行(y 轴),并在每次迭代时递增它。由于它是基于图形的,因此您还可以使用Rectangle结构并使用StringFormat对象向其打印内容以进行文本换行和布局。

打印文件

我脑海中的例子 - 未经测试。

Public Class Form1 ' your form name here
  Private count As Integer
  Private row As Integer
  Private Sub print_Page(...) Handles ...
    row = 100 'starting point from the top
    Using p As New Pen(Brushes.Bisque) 'self disposing graphics object
       'we use the variable here to know where we are if we have to go to next page
      Dim rowCount = dg.Rows.Count - 1
      For i As Integer = count To rowCount
        e.Graphics.DrawString({value},p, Font, x, y)
        row += 16 'basically the font height and some space in-between
        If row = e.MarginBounds.Bottom - 20 Then
          e.HasMorePages = True
          If i <> rowCount Then 'are we on the last row?
           count = i ' remember where we left off
           Exit Sub ' cause this event will fire again and we need to start over 
          End If
        End If
      Next
     End Using
   End Sub
   '...
End Class
于 2013-08-02T20:12:13.670 回答
0

公共类 frmTestHasMorePages

Dim Font12 As Font = New Drawing.Font("Arial", 12, FontStyle.Regular)
Dim Font8 As Font = New Drawing.Font("Arial", 8, FontStyle.Regular)
Dim sBrush As Drawing.Brush

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

    Static count As Integer
    Static topMargin As Integer = 50
    Static line As Integer
    Static s, z As Integer
    Dim linesPerPage = 65
    Dim lineCount = 350
    Static totalpages As Integer = IIf(lineCount Mod linesPerPage = 0, (lineCount / linesPerPage), (lineCount / linesPerPage) + 1)
    sBrush = Brushes.Black
    Dim pageBottom = e.PageBounds.Bottom

    For i As Integer = count To lineCount
        If z > lineCount Then
            sBrush = Brushes.Blue
            e.Graphics.DrawString("Page " & s + 1 & "/" & totalpages, Font8, sBrush, 750, pageBottom - 20)
            sBrush = Brushes.Red
            e.Graphics.DrawString("End of document ", Font8, sBrush, 50, pageBottom - 20)
            e.HasMorePages = False
            Exit Sub
        End If
        e.Graphics.DrawString("Testing hasmorepages with different options " & z, Font12, sBrush, 45, (i * 16) + topMargin)
        line += 15
        z += 1
        If i = linesPerPage AndAlso s <= totalpages Then

            e.HasMorePages = True
            sBrush = Brushes.Blue
            s += 1
            e.Graphics.DrawString("Page " & s & "/" & totalpages, Font8, sBrush, 750, pageBottom - 20)
            e.Graphics.DrawString("Continued...", Font8, sBrush, 50, pageBottom - 20)
            i = 0
            If i <> lineCount Then  REM if it is the last line to print
                Exit Sub
            End If
        End If
    Next
End Sub

结束类

于 2020-08-13T13:17:20.587 回答