0 Replies - 2974 Views - Last Post: 27 July 2012 - 12:56 PM Rate Topic: -----

#1 Ngeorge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-July 12

VB.NET Supply Cost Calculator

Posted 27 July 2012 - 12:56 PM

I need to write an application that calculates the cost of all supplies added to a user's shopping list. I have two listboxes (1:stocklist & 2:shoppinglist). The user should be able to click an item in the first listbox and click the add button to add it to the second listbox. After which, the user should hit the calculate button and the total should be displayed. I have already successfully coded the add and remove buttons. I'm confused as to how to use a for...next loop to loop through these items and calculate them.

As you can see in the attached picture, once you add an item to the listbox on the right, the item is removed from the left listbox (as it should).

Public Class SupplyCalculatorForm

   Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click

      Dim selectedItems = (From i In stockListBox.SelectedItems).ToArray()

      For Each selectedItem In selectedItems

         shoppingListBox.Items.Add(selectedItem)
         stockListBox.Items.Remove(selectedItem)
         removeButton.Enabled = True
         calculateButton.Enabled = True


      Next



   End Sub

   Private Sub removeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeButton.Click

      stockListBox.Items.Add(shoppingListBox.SelectedItem)
      shoppingListBox.Items.RemoveAt(shoppingListBox.SelectedIndex)

   End Sub

   Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click

      Dim i As Integer
      Dim prod
      Dim total
      Dim sum

      i = stockListBox.ListIndex

      prod = stockListBox.List(i)

      If stockListBox.Count > 0 Then
         shoppingListBox.Items.Add(prod)
      Else
         MsgBox("Please select item first")
      End If

      sum = 0
      For i = 0 To stockListBox.ListCount
         sum = sum + Val(stockListBox.List(i))
      Next i

      totalLabel.Text = sum

   End Sub
End Class ' SupplyCalculatorForm

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1