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.

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.