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!
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.
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?
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.
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
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
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
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
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.
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?