来自另一个表单的列表框中的Updata数据 [英] Updata data in a listbox from another form

查看:74
本文介绍了来自另一个表单的列表框中的Updata数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 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

<pre>public List<Student> studentList = new List<Student>();





public Student GetStudent(int id)//获取学生索引

{

返回studentList [id];



}



public bool UpdateStudent(int originalIndex,Student studentToEdit)

{

try

{

Student student = GetStudent (originalIndex); //选择学生索引

student.Name = studentToEdit.Name; //学生姓名

studentList.RemoveAt(originalIndex); //删除所选索引处的学生

studentList.Insert(originalIndex,student); //在索引处插入新学生。

UpdateStudentList(); //更新学生列表

}

catch {return false; }

返回true;

}





学生。 cs



公共班学生

{

public List< int>分数=新列表< int>();



公共字符串名称

{get;组; }



public bool AddScore(int score)

{

try

{

分数。加(得分);

}

catch {return false; }

返回true;

}



public List< int> GetScores()

{

返回分数;

}



public int GetScoreAt(int index)

{

return(int)得分[index];

}



public int GetScoreTotal()

{

int sum = 0;

foreach(得分中的int得分)

{

总和+ =得分;

}

返还金额;

} < br $>


public int GetScoreCount()

{

返回Scores.Count;

}



public int GetScoreAverage()

{

返回GetScoreTotal()/ GetScoreCount();

}



public void DestroyScores()

{

分数=新列表< int>();



}



我尝试过:



源代码 - GitHub - Triptonix / Student:App [< a href =https:// github.com/Triptonix/Student\"target =_ blanktitle =新窗口> ^ ]

推荐答案

您没有显示updatestudent方法,但显然你传入的索引是无效的。为什么你认为不是?
You don't show the updatestudent method but obviously the index you're passing in, is invalid. Why do you think it's not?


这篇关于来自另一个表单的列表框中的Updata数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆