title: loop through list box in vb and get a total amount
body: <p>I’m trying to loop through a list of prices on a list box and print the total in a textbox on the form. The loop I have works, but the figures it produces are all wrong!. can any one help me??</p>
  
  <p>the figures look ok but then when i add more items to the list box the total amount goes all wrong.</p>
  
  <pre><code>Public Class f
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'TODO: This line of code loads data into the 'LoftDataSet.Services' table. You can move, or remove it, as needed.
      Me.ServicesTableAdapter.Fill(Me.LoftDataSet.Services)
  
      'Loop through all the rows that are in the dataset
      For Each dr As DataRow In LoftDataSet.Services.Rows
  
          Dim btn As New Button 'Instantiate a button
          btn.Text = dr("service_name").ToString 'UserName is a field in my Users Table
          btn.Size = New Size(140, 80)
          btn.Tag = dr("ID") 'Here we set the tag to the primary key (ID)
  
          'Since we're using a flowlayoutpanel, we don't need to worry about setting the location property
          FlowLayoutPanel1.Controls.Add(btn) 'Add the button to the flow layout panel
          AddHandler btn.Click, AddressOf UserClick  'Here we give the button a handler for the click event
  
      Next
  End Sub
  
  Public Class Product
      Public Property ProductName As String
      Public Property ProductCost As Decimal
  
      Public Overrides Function ToString() As String
          Return ProductCost &amp; "      " &amp; ProductName
          'Here you could build a formatted string to the user to include cost
      End Function
  End Class
  
  'Here we write our method for the click event of the button(s) we created
  Dim SelectedProduct As New Product
  Private Sub UserClick(ByVal sender As Object, ByVal e As EventArgs)
  
      'We set a filter to the binding source passing it ID=&lt;and whatever is stored in the tag property&gt;
  
      ServicesBindingSource.Filter = "ID = " &amp; DirectCast(sender, Button).Tag.ToString
  
      SelectedProduct.ProductName = DirectCast(sender, Button).Text
      SelectedProduct.ProductCost = DirectCast(ServicesBindingSource(0), DataRowView).DataView(0)(2)
      ListBox1.Items.Add(SelectedProduct)
  
  End Sub
  
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
          ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))
      Next
  End Sub
  
  
  
  
  Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  
  End Sub
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
      Dim aValue As Decimal
      For Each item As Product In ListBox1.Items
          aValue += CDec(item.ProductCost)
      Next
      TextBox1.Text = aValue
  
  
  
  End Sub
  </code></pre>
  
  <p>End Class</p>