Public Class Form1
    Dim intsum, intaverage, counter As Integer

    Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click
        For counter = 0 To lbNumber.Items.Count - 1
            intsum += counter
        Next
        intaverage = (intsum / lbNumber.Items.Count - 1)
        lblAverage.Text = intaverage
    End Sub

    Private Sub btnSum_Click(sender As Object, e As EventArgs) Handles btnSum.Click
        For counter = 0 To lbNumber.Items.Count - 1
            intsum += counter
        Next
        lblSum.Text = intsum
    End Sub

The problem I'm having is that after pressing the Sum button the first time (which works correctly), the button can be pressed again which adds the initial sum on top of the total.

And if I press Sum first, and then Average, then the average of Sum times 2 is calculated instead. The same problem as the Sum button is present here, too.

Dani AI

Generated

Quick diagnosis and a clean fix.

The core bug is that the running total was kept at class scope and never reset, so pressing Sum again simply kept adding to the previous total. A second issue in the original logic is treating the loop index or doing arithmetic with the wrong precedence (which can accidentally subtract 1 from the result), and using Integer types causes truncation for non-integer averages. Disabling the buttons (as tried) hides the symptom but is unnecessary.

A safer approach is to compute each result from fresh local variables, parse list items to numbers, and guard against empty or non-numeric entries. Example:

Private Sub btnSum_Click(sender As Object, e As EventArgs) Handles btnSum.Click
    Dim total As Double = 0.0
    Dim value As Double
    For Each itm As Object In lbNumber.Items
        If Double.TryParse(itm.ToString(), value) Then
            total += value
        End If
    Next
    lblSum.Text = total.ToString()
End Sub

Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click
    Dim total As Double = 0.0
    Dim value As Double
    Dim count As Integer = 0
    For Each itm As Object In lbNumber.Items
        If Double.TryParse(itm.ToString(), value) Then
            total += value
            count += 1
        End If
    Next
    lblAverage.Text = If(count > 0, (total / count).ToString("F2"), "N/A")
End Sub

Short checklist:

  • Use local accumulators so each button click starts fresh.
  • Parse with TryParse to skip or report bad entries.
  • Use Double/Decimal to avoid integer truncation.
  • Be explicit with parentheses if subtracting from the denominator (e.g., total / (count - 1)) to avoid precedence errors.
  • If the app repeatedly queries the same list, consider maintaining a List(Of Double) when items change and compute from that collection.

This addresses the root causes rather than disabling the UI.

Never mind, I just added an additional intASum for Average and added .Enabled = False to both buttons so that the button can't be clicked for a second time.

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.