Need help in vb print previewing record information from file

169 Views Asked by At

My print preview code isn't working. How do i make this work. Help will be greatly appreciated. thank you. Im trying to display my final checkout receipt, i wasn't able to paste a pic so it goes like this:

Customer ID: Title: Name: Bob Address: Car: Car ID: Upgrades: Cost: Product ID: Shop ID:

I want these in a column layout in the print preview screen. How would I do it?

Also I get a warning saying: Variable 'LineToPrints' has been used before and it could result in a null value.

    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
1

There are 1 best solutions below

0
On

You haven't detailed what error you're getting, or where it is occurring, but your format string has two placeholders, and you are only passing in one argument each time. Consider using Dim MyFormat As String = "{0, 15}" if you want the label and value to fit in 15 characters, or String.Format(MyFormat, "Product ID:", OneStock.ProductID) if the label and value should be in separate columns.

NB: Consider storing the field metadata in some data structure, e.g. 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)
}

Then you can generate each line of the report as follows:

Dim line=String.Join("", metadata.Select(Function(t) String.Format("{0,15}: {1,15}", t.Item1, t.Item2))