[英]Update data in a listbox from another form
我正在創建一個維護學生成績的計划。 我創建了一個名為students的類,它存儲數據並將其顯示在列表框中。 用戶單擊添加新表單( frmAddStudent
)后,將允許用戶按名稱及其分數添加用戶,並將其顯示在主表單的列表框中。 它還允許更新/刪除功能。 我可以成功地將學生添加到列表中並進行編輯,但是當我在更新學生表單中按下確定按鈕時,我收到錯誤
System.ArgumentOutOfRangeException:'索引超出范圍。 必須是非負數且小於集合的大小。 參數名稱:index'
我查了一下,這意味着當參數的值超出被調用方法定義的允許值范圍時拋出它,但不確定它在這里是如何應用的。 我在更新時輸入的值在范圍內。
源代碼如下
https://github.com/Triptonix/Student.git
frmUpdateStudent.cs
private void UpdateButton_Click_1(object sender, EventArgs e) //open update form for current student
{
Student Form1 = new Student();
Form1.Name = StudentName.Text;
parentForm.UpdateStudent(index, Form1);
Close();
}
Form1.cs的
public List<Student> studentList = new List<Student>();
public Student GetStudent(int id) //Get student index
{
return studentList[id];
}
public void UpdateStudentList()
{
students.DataSource = null;
students.DataSource = studentList;
students.DisplayMember = "Name";
}
public bool UpdateStudent(int originalIndex, Student studentToEdit)
{
try
{
Student student = GetStudent(originalIndex); //select index of student
student.Name = studentToEdit.Name; //name of student
studentList.RemoveAt(originalIndex); //remove the student at the index selected
studentList.Insert(originalIndex, student); //insert new student at index.
UpdateStudentList(); //update student list
}
catch { return false; }
return true;
}
Student.cs
public class Student
{
public List<int> Scores = new List<int>();
public string Name { get; set; }
public bool AddScore(int score)
{
try
{
Scores.Add(score);
}
catch { return false; }
return true;
}
public List<int> GetScores()
{
return Scores;
}
public int GetScoreAt(int index)
{
return (int)Scores[index];
}
public int GetScoreTotal()
{
int sum = 0;
foreach (int score in Scores)
{
sum += score;
}
return sum;
}
public int GetScoreCount()
{
return Scores.Count;
}
public int GetScoreAverage()
{
return GetScoreTotal() / GetScoreCount();
}
public void DestroyScores()
{
Scores = new List<int>();
}
}
frmUpdateStudent
public partial class frmUpdateStudent : Form
{
private Form1 parentForm; //main form
private Student studentToEdit; //student list
private int index; //index
public frmUpdateStudent(Form1 parentForm, int index) //update parent form (Form1) with the new student and scores
{
this.parentForm = parentForm;
this.index = index;
studentToEdit = this.parentForm.GetStudent(index);
InitializeComponent();
StudentName.Text = studentToEdit.Name;
UpdateScoreDisplay();
}
public void AddScoreToStudent(int value) //add score to current student and display in the list
{
studentToEdit.AddScore(value);
UpdateScoreDisplay();
}
public void UpdateScoreAtIndex(int id, int value) //update a score selected from the list
{
studentToEdit.GetScores()[id] = value;
UpdateScoreDisplay();
}
public int GetScoreAtIndex(int id) //get the score index
{
return studentToEdit.GetScoreAt(id);
}
private void UpdateScoreDisplay() //update the score display list
{
CurrentScores.DataSource = null;
CurrentScores.DataSource = studentToEdit.GetScores();
}
private void AddScoreButton_Click(object sender, EventArgs e) //open the add score form
{
frmAddScore addScoreForm = new frmAddScore(this);
addScoreForm.Show();
}
private void RemoveScoreButton_Click_1(object sender, EventArgs e) //remove a score from current index and update display list
{
studentToEdit.GetScores().RemoveAt(CurrentScores.SelectedIndex);
UpdateScoreDisplay();
}
private void ClearScoresButton_Click_1(object sender, EventArgs e) //clear all scores
{
studentToEdit.DestroyScores();
UpdateScoreDisplay();
}
private void CloseButton_Click_1(object sender, EventArgs e)
{
Close(); //close form
}
private void UpdateButton_Click_1(object sender, EventArgs e) //open update form for current student
{
Student Form1 = new Student();
Form1.Name = StudentName.Text;
parentForm.UpdateStudent(index, Form1);
Close();
}
private void UpdateScoresButton_Click(object sender, EventArgs e)
{
frmUpdateScore updateScoreForm = new frmUpdateScore(this, CurrentScores.SelectedIndex);
updateScoreForm.Show();
}
}
事實證明,當我試圖調用它時,我的列表的索引是-1。 我將SelectedIndex設置為局部變量,然后調用它。 我想在執行之前必須檢查所選索引。 這是我修復的代碼。
private void students_SelectedIndexChanged_1(object sender, EventArgs e) {
_selectedIndex = students.SelectedIndex;
if (_selectedIndex > -1)
{
Student student = GetStudent(_selectedIndex); //select index from list
Student students = GetStudent(_selectedIndex); //select index from list
ScoreTotalTextBox.Text = student.GetScoreTotal().ToString(); //show Score Total to box
ScoreCountTextBox.Text = student.GetScoreCount().ToString(); //show Score Count to box
ScoreAverageTextBox.Text = student.GetScoreAverage().ToString(); //show Score Average to box
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.