通过另一种形式更新列表框中的数据

时间:2019-05-01 19:36:19

标签: c#

我正在创建一个维护学生分数的程序。我创建了一个名为“学生”的课程,该课程存储数据并将其显示在列表框中。用户单击添加新表单(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 个答案:

答案 0 :(得分:0)

因此,当我尝试调用它时,列表的索引为-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 
}
}