【问题标题】:How to align dot in currency while printing如何在打印时对齐货币中的点
【发布时间】:2023-04-04 08:27:02
【问题描述】:

我有这样的事情:

    Public Function ItemsToBePrinted()
    Dim p_dt As DataTable = Model_Query(2)
    Dim p_str As String = ""
    Dim StringToPrint As String = ""

    For Each drow As DataRow In p_dt.Rows
        Dim str_itemName As New String(drow.Item("item_name").ToString)
        Dim str_itemQty As New String(drow.Item("item_qty").ToString)
        Dim str_itemUnitPrice As New String(drow.Item("item_unitprice").ToString)
        Dim str_itemDisc As New String(drow.Item("item_disamt").ToString)
        Dim str_itemTotalAmt As New String(drow.Item("item_totamt").ToString)
        Dim lineLen1 As String = str_itemName.Length
        Dim lineLen2 As String = str_itemQty.Length
        Dim lineLen3 As String = str_itemUnitPrice.Length
        Dim lineLen4 As String = str_itemDisc.Length
        Dim spcLen1 As New String(" "c, 20 - lineLen1)
        Dim spcLen2 As New String(" "c, 5 - lineLen2)
        Dim spcLen3 As New String(" "c, 5 - lineLen3)
        Dim spcLen4 As New String(" "c, 8 - lineLen4)
        If drow.Item("item_disamt") = 0 Then
            StringToPrint = $"{str_itemName}{spcLen1}{str_itemQty}{spcLen2}{str_itemUnitPrice}{spcLen3}{spcLen4}{str_itemTotalAmt}"
        Else
            StringToPrint = $"{str_itemName}{spcLen1}{str_itemQty}{spcLen2}{str_itemUnitPrice}{spcLen3}{str_itemDisc}{spcLen4}{str_itemTotalAmt}"
        End If

        p_str &= StringToPrint & Environment.NewLine
    Next

    Return p_str
End Function

    Public Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim p_font As Font = New Font("Consolas", 10)

    e.Graphics.DrawString(PrintItemHeader(), p_font, Brushes.Black, 2 * 8, 305)
    e.Graphics.DrawLine(p_pen, 16, 340, 350, 340)
    e.Graphics.DrawString(ItemsToBePrinted(), p_font, Brushes.Black, 2 * 8, 345)

目前我使用 spcLen 来计算空间以使它们左对齐,但我不知道如何将对齐更改为右...

这是输出:

我怎样才能像这样对齐点?除项目代码外,所有数据都将右对齐

Item Code           Qty   Unit   Disc   Amount
                         Price
----------------------------------------------
XXXX                 33   4.70          155.10
XXXX                  2   3.00            6.00
XXXX                  2   9.00   1.80    16.20
XXXX                  1   7.50            7.50
XXXX                 11  12.00  10.00   122.00

【问题讨论】:

标签: vb.net printing alignment drawstring


【解决方案1】:

当您必须打印数字列时,您应该在数字之前而不是之后放置空格,因为您希望它们在右侧对齐

我会编写一个简单的方法,根据所需的空间和对齐方式将列数据在右侧或左侧对齐。

Function AlignText(text As String, TotalSpace As Integer, AlignRight As Boolean)

    Dim alignResult As String

    if string.IsNullOrEmpty Then
       alignResult = new String(" "c, TotalSpace)
    Else
        if text.Length > TotalSpace Then
            text = text.SubString(0, TotalSpace)
        End If
        If AlignRight Then
            alignResult = New String(" "c, TotalSpace - text.Length) & text
        Else
            alignResult = text & New String(" "c, TotalSpace - text.Length)
        End If
    End If
    Return alignResult
End Function

现在可以这样调用这个方法了

Dim str_itemName = AlignText(drow.Item("item_name").ToString, 20, False)
Dim str_itemQty = AlignText(drow.Item("item_qty").ToString, 3, True)
Dim str_itemUnitPrice = AlignText(drow.Item("item_unitprice").ToString, 10, True)
Dim str_itemDisc = AlignText(drow.Item("item_disamt").ToString), 10, True)
Dim str_itemTotalAmt = AlignText(drow.Item("item_totamt").ToString), 10, True)

当然,您应该删除循环内所有空间的计算,并且此编码也将删除 If 跳过缺少的折扣字段的需要

【讨论】:

    【解决方案2】:

    您应该使用 String.Format 函数格式化您的字符串,该函数允许您在打印时创建有用的字符串表示。查看这两个链接:

    https://www.dotnetperls.com/format-vbnet https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2015-04-15
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多