矩形的自动对齐取决于 vb.net 中的纸张大小

Automatic alignment of rectangle depend of size of paper in vb.net

我有一个矩形,它根据要打印的 stamps/receipt 数量添加另一个矩形。

我使用 for loop 添加另一个矩形,但如果我设法添加超过 3 个矩形,其他矩形将不会显示,因为它保持直线对齐,或者如果我使用 if else if e.pagebounds.widht <(我的矩形的最后一个宽度)它下降并且很好但是下一个...下一个...第 3 行的下一个及以上我怎么才能得到这是我的代码

Dim rec As Rectangle

    Dim x1, y1 As Integer
    Dim nextline As Integer
    x1 = 40
    y1 = 40
    Dim b As Integer = 0
    Dim containerrectangle As Rectangle
    containerrectangle = New Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, e.PageBounds.Height)
    For i = 0 To 6
        If e.PageBounds.Width - 100 < x1 + b Then
            nextline = 40
            'reset the x-axis of the rectangle
            y1 = 250
            b = 40
            rec = New Rectangle(b, y1 + 50, 250, 250)
            e.Graphics.DrawRectangle(Pens.Sienna, rec)
            y1 += 250
            b += 10
        Else
            rec = New Rectangle(x1 + b, 40, 250, 250)
            e.Graphics.DrawRectangle(Pens.Sienna, rec)
            x1 += 250
            b += 10
        End If
    Next

请看图https://imgur.com/a/ssv6YzX

看看这段代码 - 我会说它更直接:

    Dim RectTemplate As New Rectangle(0, 0, 250, 250)


    Dim GapPx As Int32 = 12                    ' set gap between rectangles
    Dim StaPt As New Point(GapPx, GapPx * 2)   ' set starting point
    Dim N As Int32 = 15                        ' set (or get) the totalnumber of rectangles
    Dim nL As Int32 = Math.Floor((e.PageBounds.Width - GapPx) / (RectTemplate.Width + GapPx))  ' number of rectangles per row
    Dim nR As Int32 = Math.Ceiling(N / nL)   ' number of rows of rectangles 

    For ir = 0 To nR - 1       ' process all rows
        For il = 0 To nL - 1   ' process all rectangles in a row
            Dim rect As New Rectangle(StaPt.X + il * (RectTemplate.Width + GapPx), StaPt.Y + ir * (RectTemplate.Height + GapPx), RectTemplate.Width, RectTemplate.Height)
            e.graphics.DrawRectangle(Pens.Sienna, rect)
        Next
    Next