kay so we have an activity, the activity was to make a simple cashier system using filestream.
So the layout will be there are 3 radio buttons ( foods,drinks,desserts)
so for example i chose foods the comboBox will automatically update it's value like a list
of foods. I already had this function, my problem is the i cannot get the value of it.

this is the structure of the textfile
Hamburger:10
Fries:20.4

Here's my code:

Public Class Form1
    Private Sub foodRadioBtn_CheckedChanged(sender As Object, e As EventArgs) Handles foodRadioBtn.CheckedChanged
        CalculateStuff("food.txt")
    End Sub

    Private Sub DrinksRadioBtn_CheckedChanged(sender As Object, e As EventArgs) Handles DrinksRadioBtn.CheckedChanged
        CalculateStuff("drinks.txt")
    End Sub

    Dim prices(100) As String

    Private Sub DessertsRadioBtn_CheckedChanged(sender As Object, e As EventArgs) Handles DessertsRadioBtn.CheckedChanged
        CalculateStuff("desserts.txt")
    End Sub

    Sub CalculateStuff(filePath As String)
        ComboBox1.Text = ""
        ComboBox1.Items.Clear()
        Dim lines() As String = IO.File.ReadAllLines(filePath)
        Dim count As Integer = 0
        Do
            Dim splitter() As String = lines(count).Split(":")
            ComboBox1.Items.Add(splitter(0))
            '  prices(count) = CDbl(splitter(1))
            count += 1
        Loop Until count = lines.Length
    End Sub

    Dim amount As Double = 0
    Private Sub insertBtn_Click(sender As Object, e As EventArgs) Handles insertBtn.Click
        amountLbl.ForeColor = Color.Green
        amount += CDbl(insertCashTxt.Text)
        amountLbl.Text = amount
    End Sub

    Private Sub payBtn_Click(sender As Object, e As EventArgs) Handles payBtn.Click
        MessageBox.Show("Payment Accepted")
        changeTxt.Text = amount - CDbl(totalTxt.Text)
    End Sub

    Private Sub quantityTxt_TextChanged(sender As Object, e As EventArgs) Handles quantityTxt.TextChanged
        nstextbox2.text = prices(ComboBox1.SelectedIndex) * CInt(quantityTxt.Text)
    End Sub
End Class

it is giving me an error "Index was outside the bounds of the array."

Thanks!

Recommended Answers

All 2 Replies

Make line 26: Loop Until count = lines.Length - 1

If you debugged this and stepped through it you would see that your loop is going around one time too many. Remember, you start your counter at zero but the length of lines starts at 1.

The first thing I'd suggest is a custom class(Item):

Public Class Item
    Public Enum Units
        Pound
        PerEach
        CaseLot
    End Enum
    Public Name As String = ""
    Public Price As Double = 0.0
    Public Quantity As Double = 0.0
    Public NetPrice As Double = 0.0
    Public Unit As Units = Units.PerEach
    Public Overrides Function ToString() As String
        Return Name & " - " & Price.ToString("c")
    End Function
End Class

With the ToString() method overridden like this you can now populate a combobox by assigning a List(Of Item) to the Datasource property. The items are stored as objects but it uses the ToString method to display the data:

Dim Items As List(Of Item) = New List(Of Item)
Dim ShoppingList As List(Of Item) = New List(Of Item)    

Sub CalculateStuff(filePath As String)
    ComboBox1.Text = ""
    ComboBox1.Items.Clear()
    Dim lines() As String = IO.File.ReadAllLines(filePath)
    Dim count As Integer = 0
    For Each s As String In lines
        Dim splitter() As String = s.Split(":"c)
        If splitter.Length = 2 Then
            Items.Add(New Item With {.Name = splitter(0), .Price = Double.Parse(splitter(1))})
        End If
    Next
    ComboBox1.DataSource = Items
    ComboBox1.SelectedItem = Nothing
    Button1.Enabled = False
End Sub

A button that is enabled only when a selection is made and an event handler for the comboboxes SelectionIndex_Changed event should get you on the right track:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim clickeditem As Item = DirectCast(ComboBox1.SelectedItem, Item)
    ShoppingList.Add(clickeditem)
    Button1.Enabled = False
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Button1.Enabled = True
End Sub    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.