如何使用e.HasMorePages With For Each

时间:2013-08-02 19:06:11

标签: vb.net

我使用带代码的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

2 个答案:

答案 0 :(得分:1)

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

Print Document

脱离我头顶的例子 - 未经测试。

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

答案 1 :(得分: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

结束班级