# statistics on values in list box

• (2 Pages)
• 1
• 2

## 28 Replies - 9819 Views - Last Post: 29 November 2006 - 12:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=21284&amp;s=22c09fb4bd7a53708aca81b569543237&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

# statistics on values in list box

Posted 24 November 2006 - 06:02 AM

I am working on a program where I have the user enter a course grade in a text box, and then the grade is added to a listbox. I know I can use the list.count to get the total number of grades in the list box. I also need to find the lowest, highest and average grade in the entries. I also need to assign a letter grade to each numeric grade and print the numbers of grades that are in each letter category. Can this be done in a listbox? I have not been able to find any references to doing this, so any assistance would be appreciated.

Is This A Good Question/Topic? 0

## Replies To: statistics on values in list box

### #2 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 24 November 2006 - 11:18 AM

Absolutely you can do it, however there are not any methods available to do it for you.

You will have to code the methods to read the values from your listbox and calculate the low, high, average, and perform letter grade assignments.

### #3 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 24 November 2006 - 12:27 PM

jayman9, on 24 Nov, 2006 - 11:18 AM, said:

Absolutely you can do it, however there are not any methods available to do it for you.

You will have to code the methods to read the values from your listbox and calculate the low, high, average, and perform letter grade assignments.

Thank you - at least I know I can do what I need to do with a listbox. Now I need some assistance with the code. I have the following written to date:

```	Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
'allows the user a chance to review the grade for accuracy before adding to listbox
TxtGrade.Text = " "  'removes the entered grade from the textbox so the user can enter another value
End Sub

Private Sub BtnRem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRem.Click
'to allow the user to remove any grade which was entered in error
End Sub

Private Sub BtnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCal.Click
'to count the number of items in the listbox

This works as far as I have gotten. The next thing I did was write the contents of the listbox to a file:

' copies grades entered in listbox to a file for use in doing calculations
Dim i As Integer
Dim sw As IO.StreamWriter = IO.File.CreateText("lstGrades.txt")
For i = 0 To LstGrades.Items.Count - 1
Next
sw.Close()
End Sub

```

I don't want the contents of the file to print, but to be used only in the calculations and am not sure how to do the code for that. Also, is this the best way to go with the code or is there a better way to do statistics on the listbox items?

### #4 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 24 November 2006 - 01:20 PM

Unless you have a specific reason for writing the values to a text file. It would be easier to store them in an array for use in your calculations.

The array has a sort method which can quickly sort the values and so all you have to do is pick the first and last values to determine the highest and lowest. Then you could loop through them adding them up and dividing by the count to determine the average. In addition you could use a series of If/Else or a switch statement to determine into which category each value falls and increment counters to keep track of each grade, then print the results.

### #5 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 25 November 2006 - 07:14 AM

jayman9, on 24 Nov, 2006 - 01:20 PM, said:

Unless you have a specific reason for writing the values to a text file. It would be easier to store them in an array for use in your calculations.

The array has a sort method which can quickly sort the values and so all you have to do is pick the first and last values to determine the highest and lowest. Then you could loop through them adding them up and dividing by the count to determine the average. In addition you could use a series of If/Else or a switch statement to determine into which category each value falls and increment counters to keep track of each grade, then print the results.

I have tried writing the items in the listbox to an array, but am doing something wrong as when I run the program I am told the array has a value of 0. The code I entered is:
``` 'to count the number of items in the listbox
Dim curindex As Integer
Dim intCounter As Integer
For intCounter = 0 To UBound(strgrade)
Next
For curindex = 0 To UBound(strgrade)
Next
For curindex = 0 To UBound(strgrade)
Next
LblSum.Text = total
End Sub
```

### #6 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 25 November 2006 - 10:38 AM

I am going to need to see more of your code in order to determine what could be the problem you are experiencing.

But to give you a simplified and easy idea of what you need to do. I have included a screenshot of the output so you can see the results.

The following code will load a listbox with some values in the load event. Then in the button click event it will read the values from the listbox, determine the sum of the numbers, then print the sum and the average in another textbox.
```Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim nums() As Integer = {3, 5, 6, 9, 13, 15}
Dim x As Integer
For x = 0 To 5
Next x

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim number As Integer = Me.ListBox1.Items.Count - 1
Dim y As Integer
Dim total As Integer

For y = 0 To number
total = total + Convert.ToInt16(Me.ListBox1.Items.Item(y))
Next y

Me.TextBox1.Text = "Total = " & Convert.ToString(total) & "	Average = " & Convert.ToString(total / 6)

End Sub
```

### #7 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 25 November 2006 - 10:57 AM

Correction to my previous post, I forgot you wanted to load into a new array. Sorry about that.

So here is the same concept as I described previously but stored in a new array.
```	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim nums() As Integer = {3, 5, 6, 9, 13, 15}
Dim x As Integer
For x = 0 To 5
Next x

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim number(Me.ListBox1.Items.Count) As Integer
Dim newArray As String
Dim y As Integer
Dim total As Integer

For y = 0 To Me.ListBox1.Items.Count - 1
number(y) = Convert.ToInt16(Me.ListBox1.Items.Item(y))
total = total + number(y)
Next y

Me.TextBox1.Text = "Total = " & Convert.ToString(total) & "	Average = " & Convert.ToString(total / 6)

For y = 0 To 5
newArray = newArray & Convert.ToString(number(y)) & " "
Next y

MessageBox.Show("New array has values " & newArray)

End Sub
```

### #8 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 25 November 2006 - 11:12 AM

lab3tech, on 25 Nov, 2006 - 10:45 AM, said:

jayman9, on 25 Nov, 2006 - 10:38 AM, said:

I am going to need to see more of your code in order to determine what could be the problem you are experiencing.

But to give you a simplified and easy idea of what you need to do. I have included a screenshot of the output so you can see the results.

The following code will load a listbox with some values in the load event. Then in the button click event it will read the values from the listbox, determine the sum of the numbers, then print the sum and the average in another textbox.
```Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim nums() As Integer = {3, 5, 6, 9, 13, 15}
Dim x As Integer
For x = 0 To 5
Next x

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim number As Integer = Me.ListBox1.Items.Count - 1
Dim y As Integer
Dim total As Integer

For y = 0 To number
total = total + Convert.ToInt16(Me.ListBox1.Items.Item(y))
Next y

Me.TextBox1.Text = "Total = " & Convert.ToString(total) & "	Average = " & Convert.ToString(total / 6)

End Sub
```

```
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click

'allows the user a chance to review the grade for accuracy before adding to listbox
TxtGrades.Text = " "  'removes the entered grade from the textbox so the user can enter another value
End Sub

Private Sub BtnRem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRem.Click
'to allow the user to remove any grade which was entered in error
End Sub
```

edit: fixed [code] tags ~ jayman9

### #9 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 25 November 2006 - 12:00 PM

Don't worry about how I added the data to the listbox, it is of no importance. I just did that to load some sample data, I'm too lazy to type it in each time.

The important part is retrieving the information from the listbox which hopefully I explained well enough.

What is strgrade, I see you using it but I don't see it defined anywhere?

Next thing I noticed is your storing the values in a String array. I am making the assumption that grades are entered as 0 - 4, 0 being an F and 4 being an A.

If this is the case then you need to store them in a Integer array. You will need to convert them as you retrieve them from the listbox. Otherwise if you sort it as a String array it will not sort in the manner that you would expect and you will get inaccurate results.

### #10 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 25 November 2006 - 12:57 PM

jayman9, on 25 Nov, 2006 - 12:00 PM, said:

Don't worry about how I added the data to the listbox, it is of no importance. I just did that to load some sample data, I'm too lazy to type it in each time.

The important part is retrieving the information from the listbox which hopefully I explained well enough.

What is strgrade, I see you using it but I don't see it defined anywhere?

Next thing I noticed is your storing the values in a String array. I am making the assumption that grades are entered as 0 - 4, 0 being an F and 4 being an A.

If this is the case then you need to store them in a Integer array. You will need to convert them as you retrieve them from the listbox. Otherwise if you sort it as a String array it will not sort in the manner that you would expect and you will get inaccurate results.

Your help has been much appreciated! I am getting the project completed thanks to you!! I figured out the entry of the grades via a tgextbox instead of hard coding, as well as the problems you mentioned inyour last post. The new arrayshows all the data entered in the textbox, and I have it sorted so the grades appear in increasing order, so the newe array message box shows something
like: 0 76 87 90 92 99

I know that all arrays start with a zero index, so the index of the lowest grade would be 1 and that of the highest grade would be 5, correct? So to get these grades displayed I think I would need to select the value for the 1 and 5 index. Am I thinking int he right way or has working on this all day made my mind non-functional?

### #11 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 25 November 2006 - 01:30 PM

You are correct that the index of the array starts at 0.

So the index of the first element is 0, the second element is 1, third is 2...etc.

So for an array of 6 values that are sorted, for example, the lowest value will have an index of 0 and the highest value will have an index of 5.

Just keep in mind the first index number of the first element is always 0.

### #12 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 25 November 2006 - 01:47 PM

jayman9, on 25 Nov, 2006 - 01:30 PM, said:

You are correct that the index of the array starts at 0.

So the index of the first element is 0, the second element is 1, third is 2...etc.

So for an array of 6 values that are sorted, for example, the lowest value will have an index of 0 and the highest value will have an index of 5.

Just keep in mind the first index number of the first element is always 0.

Thank you ! I will now try to get the code written correctly. Will let you know how I do!!

### #13 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 25 November 2006 - 04:46 PM

jayman9, on 25 Nov, 2006 - 01:30 PM, said:

You are correct that the index of the array starts at 0.

So the index of the first element is 0, the second element is 1, third is 2...etc.

So for an array of 6 values that are sorted, for example, the lowest value will have an index of 0 and the highest value will have an index of 5.

Just keep in mind the first index number of the first element is always 0.

If I enter grades of 98, 76, 80,76, 74 my sorted array is 0,74,76,76,80,98. Is this the way it should be? Am not really sure where the 0 in the array is coming from as I did not enter it.

### #14 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: statistics on values in list box

Posted 25 November 2006 - 04:52 PM

No you should not have a 0 in there unless you entered it in the listbox. Double check to make sure the FOR loop that is reading values from the listbox into your array starts at 0.

Other than that you may have a off-by-one logic error somewhere else in your code.

Post the latest version of code that you have if the FOR loop isn't the problem.

### #15 lab3tech

Reputation: 1
• Posts: 46
• Joined: 28-October 06

## Re: statistics on values in list box

Posted 26 November 2006 - 03:24 AM

jayman9, on 25 Nov, 2006 - 04:52 PM, said:

No you should not have a 0 in there unless you entered it in the listbox. Double check to make sure the FOR loop that is reading values from the listbox into your array starts at 0.

Other than that you may have a off-by-one logic error somewhere else in your code.

Post the latest version of code that you have if the FOR loop isn't the problem.

I cannot seem to find the problem, so I have posted the code below. I have started entering the code to get the lowest, highest and grade distribution but haven't finished that yet. Let me know if I am going in the right direction! Also, I would appreciate your guidance on another part of the code. When I initialize the array in the form load procedure, I would like to be able to set it so no values will be entered in the listbox, but whenever I try to enter anything other than a number I get an error message. I have been deleting this 90 from the list, but really don't want to have to do that. As always, thanks for all your expert assistance and your willingness to assist someone new to VB programming! I know these are easy things to do but you never make me feel dumb!!
```Private Sub BtnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCal.Click
Dim newArray As String ' to add the contents of the listbox to an array for uos in calculations
'if a grade is 89.5, newArray will show as 90 and that value will be used in calculations
Dim y, curindex As Integer
Dim average As Decimal

For y = 0 To Me.LstGrades.Items.Count - 1
total = total + number(y)
Next y

' Me.LblSum.Text = "Total = " & Convert.ToString(total) & "	Average = " & Convert.ToInt32(total / y)
Array.Sort(number)
For y = 0 To y
newArray = newArray & Convert.ToString(number(y)) & " "
Next y

For curindex = 0 To UBound(number)
If curindex = 0 Then
End If
Next
Case Is >= CStr(89.5)
count = count + 1
End Select
MessageBox.Show("New array has values " & newArray)
'Me.LblSum.Text = "Total = " & Convert.ToString(total) & "	Average = " & Convert.ToInt32(total / y)
End Sub

Private Sub TxtGrades_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtGrades.TextChanged

End Sub

Private Sub frmParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dim grade as integer = {"90"}

Dim x As Integer
For x = 0 To x