Hello, I attached my solution so far. I dont know how to create a collection within the code and read it from the .txt file.
I seriously have tried and just need to know how to create the Inventory Collection so it reads throughout the code. Please help.

Project :

Mainform on load reads inventory items from the file and updates the list box with error checking
(like Part2). Do not display items in the list box if OnHand is 0.

List box SelectedIndexChanged code:
When an item is selected in the list box, the description, retail price and units on hand boxes are
updated. The quantity is set to 1 by default and the subtotal, sales tax and grand total are empty.

Add to cart button code:
Checks to see if an item is selected in the list box and outputs an error if not.
Checks to see if the quantity is valid and outputs an error if it is not
Updates the subtotal, sales tax and grand total of purchase if quantity is valid

Complete purchase button code:
Checks to see if there is an item selected. If not, display an error message
If so, display a message box asking to confirm purchase of the item, quantity and total with yes no
buttons
If the user clicks Yes, reduce the quantity on hand and reset the form

Recommended Answers

All 4 Replies

Try changing:

Private allInventory As New Inventory

To:

 Private allInventory As New List(Of Inventory)

Add allInventory.Add(objInventory) here:

           ...

'Display data in text boxes.
DisplayInput(objInventory)

'--------------------------
' Add objInventory to list
'--------------------------
allInventory.Add(objInventory)

'Display inventory number in list box.
lstInventory.Items.Add(objInventory.InventoryNumber)

           ...

Change:

For Each InventoryNumber In allInventory
    lstInventory.Items.Add(InventoryNumber)
Next

To:

For Each item As Inventory In allInventory
    lstInventory.Items.Add(item.InventoryNumber)
Next

Change:

objInventory = CType(allInventory.Item(InventoryNumber), Inventory)

To:

 objInventory = allInventory(lstInventory.SelectedIndex)

There is an error in your constructor in "Inventory.vb".

decCost = String.Empty

"decCost" is declared as the following:

Private decCost As Decimal

decCost should be a Decimal value.

The same problem exists with "decretailPrice". Additionally, "IntqtyOnHand" is an Integer, not a String.

I don't know if this is what you meant: the Form is not picking up
the .txt and reading in the label boxes.

Imports System.IO

Public Class MainForm

Const strFILENAME As String = "Inventory.txt"
Dim dblTaxRate As Double = 8.75

Private allInventory As New List(Of Inventory)

'AllInventory.Add
'Add allInventory.Add(objInventory) here:
 Add.InventoryNumber("12345")
 Add.InventoryNumber("45638")

'Display data in text boxes.
DisplayInput(objInventory)

'Display inventory number in list box.
lstInventory.Items.Add(objInventory.InventoryNumber)


Private Sub ClearMainForm()
    lblDesc.Text = String.Empty
    lblRetail.Text = String.Empty
    lblOnHand.Text = String.Empty

End Sub


Private Sub UpdateListBox()
    lstInventory.Items.Clear()


    For Each Item As Inventory In allInventory
        lstInventory.Items.Add(Item.InventoryNumber)
    Next
    If lstInventory.Items.Count > 0 Then
        lstInventory.SelectedIndex = 0
    Else
        ClearMainForm()
    End If

End Sub


Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim objInventory As New Inventory
    Dim inventoryFile As System.IO.StreamReader
    Dim blnFound As Boolean = False


    ' Open the file.
    If System.IO.File.Exists(strFILENAME) Then

        inventoryFile = System.IO.File.OpenText(strFILENAME)

        UpdateListBox()

        'Enter loop and read till end of file.
        Do Until (inventoryFile.Peek = -1) Or blnFound

            'Read lines from file, save into Inventory object properties.
            objInventory.InventoryNumber = inventoryFile.ReadLine
            objInventory.Description = inventoryFile.ReadLine
            objInventory.PartCost = inventoryFile.ReadLine
            objInventory.Retail = inventoryFile.ReadLine
            objInventory.OnHand = inventoryFile.ReadLine


        Loop
        'Close the file.
        inventoryFile.Close()

        If blnFound Then


            'Display data in text boxes.
            DisplayInput(objInventory)

            'Display inventory number in list box.
            lstInventory.Items.Add(objInventory.InventoryNumber)
        End If
    End If

End Sub




Private Sub DisplayInput(ByVal objInventory As Inventory)


    lblDesc.Text = objInventory.Description
    lblRetail.Text = objInventory.Retail.ToString
    lblOnHand.Text = objInventory.PartCost.ToString

End Sub





Private Sub lstInventory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstInventory.SelectedIndexChanged

    Dim objInventory As New Inventory

    If lstInventory.SelectedIndex <> -1 Then

        'Retrieve student's data from inventoryCollection. Convert object into Inventory object.
        Try

            objInventory = allInventory(lstInventory.SelectedIndex)

            'Display data in labels data stored in inventory object.
            DisplayInput(objInventory)

        Catch ex As Exception
            'Display error message.
            MessageBox.Show(ex.Message)
        End Try
    End If
End Sub


Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    ClearMainForm()
End Sub

Private Sub btnExits_Click(sender As Object, e As EventArgs) Handles btnExits.Click
    Me.Close()
End Sub

End Class

Sorry, I'm not going to debug your whole program for you. Place "MessageBox.Show()" and/or "Console.WriteLine()" statements throughout your code to help you find out what is executing and what is not.

ex:

MessageBox.Show("Before if 1")
Console.WriteLine("Before if 1")
    ...
MessageBox.Show("Inside if 1")
Console.WriteLine("Inside if 1")
    ...

MessageBox.Show("After if 1")
Console.WriteLine("After if 1")

    ...

Still unable to figure this out.So frustrating. I redid the code to no avail.

Imports System.IO


Public Class MainForm

    Const strFILENAME As String = "Inventory.txt"
    Dim InventoryCollection As New Collection


    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim InventoryFile As StreamReader
        Dim objinventory As New Inventory



        Try
            InventoryFile = File.OpenText("Inventory.txt")

            'Enter loop and read till end of .txt file.
            Do Until InventoryFile.Peek = -1

                objinventory = New Inventory

                'Read lines from .txt file, save into object properties.
                objinventory.InventoryNumber = InventoryFile.ReadLine
                objinventory.Description = InventoryFile.ReadLine
                objinventory.PartCost = InventoryFile.ReadLine
                objinventory.Retail = InventoryFile.ReadLine
                objinventory.OnHand = InventoryFile.ReadLine


                'Display data in text boxes.
                lstInventory.Items.Add(objinventory.InventoryNumber)
            Loop

            'Close the file.
            InventoryFile.Close()
        Catch ex As Exception
            'Display error message.
            MessageBox.Show(ex.Message)

        End Try


    End Sub

    Private Sub lstInventory_SelectedItemChanged(sender As Object, e As EventArgs) Handles lstInventory.SelectedIndexChanged
        Dim objInventory As Inventory


        'See if an Item is Selected
        If lstInventory.SelectedItem <> -1 Then


            'Retrieve student's data from inventoryCollection. Convert object into Inventory object.
            Try

                objInventory = CType(InventoryCollection.Item(lstInventory.SelectedItem), Inventory)




            Catch ex As Exception
                'Display error message.
                MessageBox.Show(ex.Message)

            End Try
        End If
    End Sub
End Class
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.