I'm trying to get the addButton to work correctly in this code, but it keeps only outputing the first number entered, not the sum of the numbers. What am I missing here?

Public Class ArithmeticCalculatorForm
   Dim numCounter As Double = 0
   Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

      If resultLabel.Text <> "" Then
         resultLabel.Text = ""
         operandListBox.Items.Clear()
         addButton.Enabled = False
         multiplyButton.Enabled = False
      End If

      'display operands in LBox
      operandListBox.Items.Add(Val(inputTextBox.Text))
      inputTextBox.Clear()
      inputTextBox.Focus()

      If operandListBox.Items.Count < 2 Then
         addButton.Enabled = False
         multiplyButton.Enabled = False
      Else
         addButton.Enabled = True
         multiplyButton.Enabled = True
      End If
   End Sub

   Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
      Dim total As Double = 0
      Dim numCounter As Integer = 0
      Dim eachNum As Double = 0

      eachNum = operandListBox.Items.Item(numCounter)
      total += eachNum    'add each number to total
      numCounter += 1

      resultLabel.Text = String.Format("{0:F}", total)
      addButton.Enabled = True            'enable Add Grade button
      inputTextBox.Focus()

   End Sub
End Class ' ArithmeticCalculatorForm

You should use a loop from that loop from the first item in the list to the n-th.

Edited 5 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.