在 vb.net 中打印收据

Print receipt in vb.net

我正在尝试使用 vb.net 打印上述收据。但是,如图所示,金额数字出现在新的一行中。我做错了什么?

下面是我使用的代码:

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

        Dim offset As Integer = 15
    Dim itemdesc As String = "Item".PadRight(30)
        Dim Pricedesc As String = "Price".PadRight(8)
        Dim qtydesc As String = "Qty".PadRight(5)
        Dim amtdesc As String = "Amount"
        Dim descline As String = itemdesc + Pricedesc + qtydesc + amtdesc
        e.Graphics.DrawString(descline, New Font("Courier New", 8, FontStyle.Regular), Brushes.Black, 25, 100 + offset)
        offset += 10
    Dim startx As Integer = 25
        Dim starty As Integer = 100

        Dim font As New Font("Courier New", 8, FontStyle.Regular)
        Dim item As String
        Dim price As String
        Dim qty As String
        Dim amount As String

    Dim productline As String
        For k As Integer = 0 To dgvSales.RowCount - 2

            item = dgvSales.Rows(k).Cells(1).Value.ToString.PadRight(30)
            price = dgvSales.Rows(k).Cells(3).Value.ToString.PadRight(8)
            qty = dgvSales.Rows(k).Cells(4).Value.ToString.PadRight(5)
            amount = dgvSales.Rows(k).Cells(5).Value.ToString
            productline = item + price + qty + amount
            e.Graphics.DrawString(productline, font, Brushes.Black, startx, starty + offset)
            offset += 20

        Next
End Sub

三种可能:

  • 您的列数量(列 idx 4)包含一个包含换行符的字符串。如果是这种情况,您可以在加入值之前 Trim() 值。
  • 您的金额栏包含很多空格,或以换行符开头。如果是这种情况,您也可以通过使用 Trim() 来修复它,这次是在加入值之前在变量 amount 上。
  • 还有一点我还没想到:).