繁体   English   中英

从另一个表单更新列表框中的数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM