hello all, so i am taking a 1 hour vb class, and i am having trouble printing formated output to a list box. Basically I have two string variables that i use to format the output, but it is not working. Any clues? thanks

Public Class Form1

    Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click
        'make the output area visible to the user
        outPut_gp3.Visible = True
        output.Visible = True

        'declare variables
        Dim val_Pizza, val_Fries, val_Drinks, amt_Pizza, amt_Fries, amt_Drinks, total_Bill As Double
        Dim priceFile, line, a As String
        Dim formatOut1 As String = "{0,4} {16,23} {27,31}"
        Dim formatOut2 As String = "{0,12} {16,23} {27,31}"
        Dim val_Pline, val_Fline, val_Dline As Double

        'set filename
        priceFile = fileName.Text

        'read datafile, set values
        Dim filein As IO.StreamReader = IO.File.OpenText(priceFile)
        line = filein.ReadLine
        a = line.IndexOf(" ")
        val_Pizza = line.Substring(0, a)
        line = line.Substring(a + 1)
        val_Fries = line.Substring(0, a)
        line = line.Substring(a + 1)
        val_Drinks = line

        'set item ammounts
        amt_Pizza = numPizza.Text
        amt_Fries = numFries.Text
        amt_Drinks = numDrinks.Text

        val_Pline = (val_Pizza) * (amt_Pizza)
        val_Fline = (val_Fries) * (amt_Fries)
        val_Dline = (val_Drinks) * (amt_Drinks)

        total_Bill = val_Pline + val_Fline + val_Dline

        'Declare strings for printing, set equal to calculated values
        Dim nP, nF, nD, vP, vF, vD, vTot As String

        nP = amt_Pizza
        nF = amt_Fries
        nD = amt_Drinks
        vP = FormatCurrency(val_Pline, 2)
        vF = FormatCurrency(val_Fline, 2)
        vD = FormatCurrency(val_Dline, 2)
        vTot = FormatCurrency(total_Bill, 2)
        '{0,4} {16,23} {27,31}
        'Print Headings to output
        output.Items.Add(String.Format(formatOut1, "ITEM", "QUANTITY", "PRICE"))
        'Print Data to output
        output.Items.Add(String.Format(formatOut2, "Pizza Slices", nP, vP))
        output.Items.Add(String.Format(formatOut2, "Fries", nF, vF))
        output.Items.Add(String.Format(formatOut2, "Soft Drinks", nD, vD))
        output.Items.Add(String.Format(formatOut2, "TOTAL", " ", vTot))

        'This is working
        'output.Items.Add("Pizza Slices  ")
        'output.Items.Add("  ")

    End Sub
End Class

First, You post on wrong section, this for vb4/5/6 not for vb.net

Dim formatOut1 As String = "{0,4} {16,23} {27,31}"
Dim formatOut2 As String = "{0,12} {16,23} {27,31}"

Your format string is out of index.
You have 3 items in your format string. The index of those 3 variables start at 0, for 3 items, the largest index you would have is 2.
see your format, the largest index is 27.

so it should be like this :

Dim formatOut1 As String = "{0,4} {1,23} {2,31}"
Dim formatOut2 As String = "{0,12} {1,23} {2,31}"

hope it helps.
marks as solved if it's done.

This article has been dead for over six months. Start a new discussion instead.