vb.net 使用 printdocument 打印多页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16945172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:51:18  来源:igfitidea点击:

Printing multiple pages with printdocument

vb.netvisual-studio-2010printdocument

提问by Zebriukas Dry?iukas

I'm making program, which: After you select few rows in datagridview it checks if 1 or more was selected. If one, print two copies of report on one page(One report = half page) If more: Print two reports per page, printing as much as needed pages. Problem is, my code prints 4531456453 pages of same report(first and second row) :/

我正在制作程序,其中:在 datagridview 中选择几行后,它会检查是否选择了 1 行或更多行。如果有,在一页上打印两份报告(一份报告=半页) 如果更多:每页打印两份报告,打印所需页数。问题是,我的代码打印了 4531456453 页的相同报告(第一行和第二行):/

Basic example of code:

代码的基本示例:

yPos = 0
Do While tmpI < mydatagridview.SelectedRows.Count - 1
For Each selectedrow As DataGridViewRow In mydatagridview.SelectedRows
    Dim data as string = mydatagridview.SelectedRows(selectedrow.index).cells(1).value
    Dim data2 as string = mydatagridview.SelectedRows(selectedrow.index).cells(12).value

    e.graphics.drawstring(data, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data, drawfont).width/2), 25+yPos)
    e.graphics.drawstring(data2, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data2, drawfont).width/2), 50+yPos)
    yPos += e.pagebounds.height/2
    tmpI += 1 
    If yPos > e.pagebound.height/2 Then
        h = 0
        e.HasMorePages = true
        Exit Sub
    End If
Next selecedrow
Loop

As of right now, as I said before it prints infinite amount of pages having data and data2 from SelectedRows with indexes 0 and 1.

截至目前,正如我之前所说,它从索引为 0 和 1 的 SelectedRows 打印无限量的具有 data 和 data2 的页面。

回答by matzone

Hope this helps ...............

希望这可以帮助 ...............

Sub PrintIt(ByVal e As System.Drawing.Printing.PrintPageEventArgs, byval nRow as Integer,ByVal nY As Integer) 

    Dim data as string = mydatagridview.SelectedRows(nRow).cells(1).value
    Dim data2 as string = mydatagridview.SelectedRows(nRow).cells(12).value

    e.graphics.drawstring(data, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data, drawfont).width/2), 25+nY)
    e.graphics.drawstring(data2, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data2, drawfont).width/2), 50+nY)

End Sub

And some modif in your code ..

和你的代码中的一些修改..

yPos = 0

If mydatagridview.SelectedRows.Count = 1

    PrintIt(e,0,yPos)

    yPos += e.pagebounds.height/2

    PrintIt(e,0,yPos)

Elseif mydatagridview.SelectedRows.Count > 1

Dim x,n As Integer

    For x = 0 to mydatagridview.SelectedRows.Count-1
        If n = 2 Then
             e.HasMorePages = true
             n = 0
             yPos = 0
         End If 

         PrintIt(e,x,yPos)
         yPos += e.pagebounds.height/2

         n += 1
    Next

End If