Public Class frmClothingInventory 'Creating the collection
Dim inventorylist As New List(Of Clothes)
'FORM LOAD: when opened, the collection is created and the data from the text file is stored
Private Sub frmClothingInventory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Adding data from the text file to the list
Dim sr As StreamReader 'Opening the file to read
sr = File.OpenText("Project1Data.txt")
'Creating variable to be able to read through the text file
Do Until sr.EndOfStream 'Reading through the file until the end of the textfile
line = sr.ReadLine 'makes each line of the textfile into a variable
If Not line = Nothing Then
data = line.Split(","c) 'Splits the data in the text file by a comma
Dim myclothes As New Clothes(data(0), data(1), data(2), data(3), data(4), data(5), data(6)) 'Adding the data as a line to the list
inventorylist.Add(myclothes)
Else : MessageBox.Show("There is no data in the text file")
'Adding the data to the list box to be displayed
lstClothes.DataSource = Nothing
lstClothes.DataSource = inventorylist
'ADD BUTTON: the user is able to add objects to the inventory collection and the text file
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
'Making sure data type is correct and not empty
If validatedata() = False Then
'Call overloaded constructor to create object of the class
Dim myclothes As New Clothes(txtBrand.Text, txtColor.Text, txtName.Text, txtSize.Text,
CInt(txtSku.Text), CInt(txtQuantity.Text), CDbl(txtCost.Text))
'Adding the clothes to the Array
inventoryList.Add(myclothes)
'Adding the clothes to the data file
sw = File.AppendText("Project1Data.txt")
'Showing the user a message box to aware them the item has been added
MessageBox.Show("Your item has been successfully added to the inventory")
'Clearing the textboxes and the list box to get ready to add another vehicle
lstClothes.DataSource = Nothing
Dim toolTip1 As New ToolTip()
toolTip1.ShowAlways = True
toolTip1.SetToolTip(Me.btnEdit, "Be sure to search for an item before you edit")
'EDIT BUTTON: the user is able to edit an existing object from the inventory collection and text file
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
'Making sure the dat entered for editing is valid
If validatedata() = False Then
'The data in the textboxes are entered into the list
Dim mynewclothes As New Clothes(txtBrand.Text, txtColor.Text, txtName.Text, txtSize.Text,
CInt(txtSku.Text), CInt(txtQuantity.Text), CDbl(txtCost.Text))
'Finding the index of the item you want to replace
Dim index As Integer = inventorylist.IndexOf(mynewclothes)
'If the index is not -1 then that means the item is found in the list
inventorylist.Insert(index, mynewclothes) 'inserts the data that was changed in the textboxes
inventorylist.RemoveAt(index + 1) 'Removes the old line of data
'A message appear to let the user know the item has been edited
MessageBox.Show(" The " & txtColor.Text & " " & txtBrand.Text & " " & txtName.Text & " has been edited in the inventory")
'Adding the clothes to the data file
sr = File.OpenText("Project1Data.txt")
Dim sw As IO.StreamWriter = IO.File.CreateText("temp.txt")
'If the item is in the inventory list then add it to the textfile
For Each item In inventorylist
IO.File.Delete("Project1Data.txt")
IO.File.Move("temp.txt", "Project1Data.txt")
IO.File.Delete("temp.txt")
'Clearing the textboxes and list box to get ready to edit another vehicle
'DISPLAY ALL BUTTON: the user can display all objects in the inventory collection
' Sorted in ABC order by the compare to method
Private Sub btnDisplayAll_Click(sender As Object, e As EventArgs) Handles btnDisplayAll.Click
'Calls the compare function to sort the list in ABC order
If radBrand.Checked = True Then
inventorylist.Sort(AddressOf compareByBrand)
ElseIf radCost.Checked = True Then
inventorylist.Sort(AddressOf compareByCost)
ElseIf radBrand.Checked = False And radCost.Checked = False Then
inventorylist.Sort(AddressOf compareByBrand)
'Displaying all the data in the list in the listbox
lstClothes.DataSource = Nothing
lstClothes.DataSource = inventorylist
'DELETE BUTTON: the user can delete objects from the inventory collection and text file
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'Removing an clothes off the inventory list
Dim int As Integer = lstClothes.SelectedIndex
inventorylist.RemoveAt(int)
'Removing clothes out of the inventory data file
sr = File.OpenText("Project1Data.txt")
Dim sw As IO.StreamWriter = IO.File.CreateText("temp.txt")
For Each m In inventorylist
IO.File.Delete("Project1Data.txt")
IO.File.Move("temp.txt", "Project1Data.txt")
IO.File.Delete("temp.txt")
lstClothes.DataSource = Nothing
lstClothes.DataSource = inventorylist
'SEARCH BUTTON: the user can search and display a specific object of the class from the collection
' This function is implemented by the equality method
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
lstClothes.DataSource = Nothing
Dim s As New Clothes(txtSearch.Text)
Dim index As Integer = inventorylist.IndexOf(s) 'Finds the index of the brand entered in the textbox
If inventorylist.Contains(s) Then 'If the index matches then
s = CType(inventorylist(index), Clothes)
lstClothes.Items.Add(inventorylist(index).ToString) 'Adds the matched brand data to the list box
'Adds all the data that matches the brand name into the textboxes
txtQuantity.Text = s.quantity
'If the brand name did not match then a message box appears
MessageBox.Show("The item you entered was not found")
txtSearch.Text = "" 'Clears the item you searched
'VALIDATE DATA: to ensure the user enteres correct data in the text boxes
Private Function validatedata() As Boolean
'Checking each textbox to see if the user entered something or entered in the correct data type
If (txtBrand.Text) = "" Then
MessageBox.Show("Please enter a Name Band")
ElseIf (txtColor.Text) = "" Then
MessageBox.Show("Please enter a color")
ElseIf (txtName.Text) = "" Then
MessageBox.Show("Please enter shirt, pant, skirt, or dress")
ElseIf (txtSize.Text) = "" Then
MessageBox.Show("Please enter a size")
ElseIf (txtSku.Text) = "" Or Not IsNumeric(txtSku.Text) Then
MessageBox.Show("Please enter a sku")
ElseIf (txtQuantity.Text) = "" Or Not IsNumeric(txtQuantity.Text) Then
MessageBox.Show("Please enter a quantity")
ElseIf (txtCost.Text) = "" Or Not IsNumeric(txtCost.Text) Then
MessageBox.Show("Please enter a cost")
'COMPARE BRAND: Sorts the output or collection by brand
Private Function compareByBrand(ByVal x As Clothes, ByVal y As Clothes) As Integer
'Comparing the brand to be able to sort
Return x.brand.CompareTo(y.brand)
'COMPARE COST: Sorts the output or collection by the cost
Private Function compareByCost(ByVal x As Clothes, ByVal y As Clothes) As Integer
'Comparing the brand to be able to sort
Return x.cost.CompareTo(y.cost)
'EXTRA CREDIT: Using a linq query to show the calculated money that will be sold if all items are solf
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnCal.Click
'Creating the query and telling it to get the data from the
' Setting variables from the collection, determining if the calculated money is greater than 100
' Ordering the list by the money calculated
Dim moneyquery = From item In inventorylist
Let quantity = item.quantity
Let money = item.CalcMoney
Select brand, name, money
'Displaying the output in the listbox
lstClothes.Visible = True
lstClothes.DataSource = Nothing
lstClothes.DataSource = moneyquery.ToList()