statistics on values in list box

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 8539 Views - Last Post: 29 November 2006 - 12:29 AM Rate Topic: -----

#1 lab3tech  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 24 November 2006 - 12:27 PM

View Postjayman9, 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
		grade = TxtGrade.Text
		'allows the user a chance to review the grade for accuracy before adding to listbox
		LstGrades.Items.Add(TxtGrade.Text)
		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
		LstGrades.Items.Remove(LstGrades.SelectedItem)
	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
		total = LstGrades.Items.Count

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

 Sub SaveLstGrades()
		' 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
			sw.WriteLine(LstGrades.Items(i))
		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?

Thanks for your assistance.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 25 November 2006 - 07:14 AM

View Postjayman9, 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
		total = LstGrades.Items.Count
		Dim curindex As Integer
		Dim strallgrades() As String
		Dim intCounter As Integer
		LstGrades.Items.Add(" ")
		For intCounter = 0 To UBound(strgrade)
			strallgrades(intCounter) = LstGrades.Items.Item(intCounter)
		Next
		For curindex = 0 To UBound(strgrade)
			Debug.WriteLine(strgrade(curindex))
		Next
		Array.Sort(strgrade)
		Array.Reverse(strgrade)
		For curindex = 0 To UBound(strgrade)
			Debug.WriteLine(strgrade(curindex))
		Next
   LblSum.Text = total
	End Sub


Thanks for any help you can provide!
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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
			Me.ListBox1.Items.Add(nums(x))
		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

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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
			Me.ListBox1.Items.Add(nums(x))
		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

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 25 November 2006 - 11:12 AM

View Postlab3tech, on 25 Nov, 2006 - 10:45 AM, said:

View Postjayman9, 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
			Me.ListBox1.Items.Add(nums(x))
		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


I really appreciate all your excellent assistance. Your code works great when I add it to the program. However, I cannot put the grade values into the code - the user must enter them on the form. I also do not know how many grades they will be entering. Because of this I was entering the grades in a textbox, and then clicking an add button to add the grades to the listbox. I also needed the user to be able toremove a grade if one was entered in error, so I coded a remove button. Once I get the grades in an array, I need to sort to get the highest and lowest, then determine how many of the grades are in set ranges. The code I wrote for the addition of the grades tothe program, and for the remove button, is below. The other code was sent earlier. Again, many thanks for all your much appreciated assistance.

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

		LstGrades.Items.Add(TxtGrades.Text)
		'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
		LstGrades.Items.Remove(LstGrades.SelectedItem)
	End Sub


edit: fixed [code] tags ~ jayman9
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 25 November 2006 - 12:57 PM

View Postjayman9, 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?
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 25 November 2006 - 01:47 PM

View Postjayman9, 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!!
Was This Post Helpful? 0
  • +
  • -

#13 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 25 November 2006 - 04:46 PM

View Postjayman9, 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.
Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#15 lab3tech  Icon User is offline

  • New D.I.C Head

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

Re: statistics on values in list box

Posted 26 November 2006 - 03:24 AM

View Postjayman9, 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 number(Me.LstGrades.Items.Count) As Integer
		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
		Dim gradeA, gradeB, gradeC, gradeD, gradeF, count, lowgrade, highgrade As Integer
		Dim grade As String

		For y = 0 To Me.LstGrades.Items.Count - 1
			number(y) = Convert.ToInt32(Me.LstGrades.Items.Item(y))
			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
				lowgrade = newArray.IndexOf("0")
			End If
		Next
		grade = newArray
		Select Case grade
			Case Is >= CStr(89.5)
				count = count + 1
				gradeA = count
		End Select
		MessageBox.Show("New array has values " & newArray)
		Me.LblSum.Text = CStr(gradeA)
		'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
		TxtGrades.Focus()
		
	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
			Me.LstGrades.Items.Add(grade(x))
		Next x
	End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2