我的打印预览代码不起作用。我该如何做到这一点。帮助将不胜感激。谢谢。我试图显示我的最终结帐收据,我无法粘贴图片,所以它是这样的:
客户编号:标题:姓名:鲍勃地址:汽车:汽车识别码:升级:成本:产品编号:店铺编号:
我希望这些在打印预览屏幕的列布局中。我该怎么做?
我还收到一条警告:变量"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
您尚未详细说明您遇到的错误或发生错误的位置,但您的格式字符串有两个占位符,并且您每次只传入一个参数。如果希望标签和值适合 15 个字符,请考虑使用 Dim MyFormat As String = "{0, 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))