Hayden Meherg

Hayden Meherg

  • NA
  • 35
  • 6k

How to update data in a listbox from another form?

May 1 2019 2:47 PM
I'm creating a program that maintains student scores. I've created a class called students that stores the data and displays it in a list box. Once the user clicks btnAdd a new form (frmAddStudent) loads that allows them to add the user by name and their scores and display it in the listbox in the main form. It also allows the update/delete functions. I can succesfully add students to the list and edit them, but when I press the ok button in the update students form I get the error,
 
"System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
 
Parameter name: index'". I know this means that the argument passed was out of range, but I'm not sure how. Any help is appreciated. Thanks! I've included my source code if you want a deeper look.
frmUpdateStudent.cs
  1. private void UpdateButton_Click_1(object sender, EventArgs e) //open update form for current student  
  2. {  
  3. Student Form1 = new Student();  
  4. Form1.Name = StudentName.Text;  
  5.                parentForm.UpdateStudent(index, Form1);  
  6. Close();  
  7.   
  8. }
Form1.cs
  1. public Student GetStudent(int id) //Get student index  
  2. {  
  3. return studentList[id];  
  4. }  
Student.cs
  1. public class Student  
  2. {  
  3. public List<int> Scores = new List<int>();  
  4.   
  5. public string Name  
  6. getset; }  
  7.   
  8. public bool AddScore(int score)  
  9. {  
  10. try  
  11. {  
  12. Scores.Add(score);  
  13. }  
  14. catch { return false; }  
  15. return true;  
  16. }  
  17.   
  18. public List<int> GetScores()  
  19. {  
  20. return Scores;  
  21. }  
  22.   
  23. public int GetScoreAt(int index)  
  24. {  
  25. return (int)Scores[index];  
  26. }  
  27.   
  28. public int GetScoreTotal()  
  29. {  
  30. int sum = 0;  
  31. foreach (int score in Scores)  
  32. {  
  33.                sum += score;  
  34. }  
  35. return sum;  
  36. }  
  37.   
  38. public int GetScoreCount()  
  39. {  
  40. return Scores.Count;  
  41. }  
  42.   
  43. public int GetScoreAverage()  
  44. {  
  45. return GetScoreTotal() / GetScoreCount();  
  46. }  
  47.   
  48. public void DestroyScores()  
  49. {  
  50. Scores = new List<int>();  
  51.   
  52. }  

Answers (2)