Welcome to Dream.In.Code
Become a VB Expert!

Join 137,264 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,503 people online right now. Registration is fast and FREE... Join Now!




statistics on values in list box

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

statistics on values in list box

lab3tech
24 Nov, 2006 - 05:02 AM
Post #1

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
24 Nov, 2006 - 10:18 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Statistics On Values In List Box
24 Nov, 2006 - 11:27 AM
Post #3

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(jayman9 @ 24 Nov, 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.


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:

CODE

    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.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
24 Nov, 2006 - 12:20 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Statistics On Values In List Box
25 Nov, 2006 - 06:14 AM
Post #5

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(jayman9 @ 24 Nov, 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.



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:
CODE
'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!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
25 Nov, 2006 - 09:38 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
CODE

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 thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
25 Nov, 2006 - 09:57 AM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
CODE

    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 thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Statistics On Values In List Box
25 Nov, 2006 - 10:12 AM
Post #8

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(lab3tech @ 25 Nov, 2006 - 10:45 AM) *

QUOTE(jayman9 @ 25 Nov, 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.
CODE

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.

CODE
  
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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
25 Nov, 2006 - 11:00 AM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.

User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Statistics On Values In List Box
25 Nov, 2006 - 11:57 AM
Post #10

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(jayman9 @ 25 Nov, 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.



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?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Statistics On Values In List Box
25 Nov, 2006 - 12:30 PM
Post #11

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

lab3tech
RE: Statistics On Values In List Box
25 Nov, 2006 - 12:47 PM
Post #12

New D.I.C Head
*

Joined: 28 Oct, 2006
Posts: 46


My Contributions
QUOTE(jayman9 @ 25 Nov, 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.



Thank you ! I will now try to get the code written correctly. Will let you know how I do!!
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:21PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month