Total Pageviews

Thursday, November 10, 2016

Calculate average of numbers in a ListBox in Visual Basic 2015

Calculate average of numbers in a ListBox in Visual Basic 2015
Calculate average of numbers in a ListBox in Visual Basic 2015
Calculating averages of a set of numbers in ListBox control in Visual Basic is very easy task. Because listbox stores strings we should convert the data in the listbox. Before we calculate the average we have to determine all the numbers or total and the current count.

Here is the source code:

        Dim AllNumbers As Double = 0
        Dim Current As Double = 0
        Dim Average As Double = 0
        For i As Integer = 0 To ListBox1.Items.Count - 1
            AllNumbers += Val(ListBox1.Items(i))
            Current += 1
        Next
        Average = AllNumbers / Current
        TextBox2.Text = Average.ToString

No comments:

Post a Comment