0

我的打印预览代码不起作用。我如何使这项工作。帮助将不胜感激。谢谢你。我试图显示我的最终结帐收据,我无法粘贴图片,所以它是这样的:

客户 ID: 职务: 姓名: Bob 地址: 汽车: 汽车 ID: 升级: 成本: 产品 ID: 商店 ID:

我希望这些在打印预览屏幕的列布局中。我该怎么做?

我还收到一条警告:以前使用过变量“LineToPrints”,它可能导致空值。

    Dim TitleFont As New Font("Courier New", 15, FontStyle.Bold)
    Dim MyFont As New Font("Courier New", 12, FontStyle.Regular)
    Dim MyFormat As String = "{0, 15}{1,15}"
    Dim LineToPrints As String
    Dim X As Integer
    Dim Y As Integer
    X = 15

    FontHeight = MyFont.GetHeight(e.Graphics)
    e.Graphics.DrawString("Checkout Receipt", TitleFont, Brushes.Black, X, Y)

    For Y = 15 To 165 Step 15
        Select Case Y
            Case 30
                LineToPrints = String.Format(MyFormat, "Customer ID: " & OneCustomer.CustomerID)
            Case 45
                LineToPrints = String.Format(MyFormat, "Title: " & OneCustomer.Title)
            Case 60
                LineToPrints = String.Format(MyFormat, "Name: " & Trim(OneCustomer.FirstName) & Trim(OneCustomer.LastName))
            Case 75
                LineToPrints = String.Format(MyFormat, "Address: " & OneCustomer.Address)
            Case 90
                LineToPrints = String.Format(MyFormat, "Car(s): " & OneBooking.Car)
            Case 105
                LineToPrints = String.Format(MyFormat, "Car ID: " & OneBooking.CarID)
            Case 120
                LineToPrints = String.Format(MyFormat, "Upgrades: " & OneBooking.Upgrade)
            Case 135
                LineToPrints = String.Format(MyFormat, "Cost: " & FormatCurrency(TotalCost))
            Case 150
                LineToPrints = String.Format(MyFormat, "Product ID: " & OneStock.ProductID)
            Case 165
                LineToPrints = String.Format(MyFormat, "ShopID: " & OneStock.ShopID)
        End Select
        e.Graphics.DrawString(LineToPrints, TitleFont, Brushes.Black, X, Y + 10)
        e.Graphics.DrawString("Thank You For Purchasing At 'Ford Mustaang Selection Buyout'.", TitleFont, Brushes.Black, X, Y)
    Next
4

1 回答 1

0

你没有详细说明你得到了什么错误,或者它发生在哪里,但是你的格式字符串有两个占位符,你每次只传递一个参数。Dim MyFormat As String = "{0, 15}"如果您希望标签和值适合 15 个字符,或者String.Format(MyFormat, "Product ID:", OneStock.ProductID)标签和值应该在不同的列中,请考虑使用。

注意:考虑将字段元数据存储在某些数据结构中,例如List(Of Tuple(Of String,Object))

Dim metadata = New List(Of Tuple(Of String, Object)) From {
    Tuple.Create("Customer ID", OneCustomer.CustomerID),
    Tuple.Create("Title", OneCustomer.Title),
    Tuple.Create("Name", CType(Trim(OneCustomer.FirstName) & Trim(OneCustomer.LastName),Object)),
    Tuple.Create("Address", OneCustomer.Address),
    Tuple.Create("Car(s)", OneBooking.Car),
    Tuple.Create("Car ID", OneBooking.CarID),
    Tuple.Create("Upgrades", OneBooking.Upgrade),
    Tuple.Create("Cost", CType(FormatCurrency(TotalCost),Object)),
    Tuple.Create("Product ID", OneStock.ProductID),
    Tuple.Create("Shop ID", OneStock.ShopID)
}

然后您可以生成报告的每一行,如下所示:

Dim line=String.Join("", metadata.Select(Function(t) String.Format("{0,15}: {1,15}", t.Item1, t.Item2))
于 2013-12-22T23:42:26.543 回答