将姓名和分数传给另一个表格(C#)
本文关键字:表格 另一个 | 更新日期: 2023-09-27 18:20:38
我正试图将用户输入从我的AddNewStudent表单传递到我的主StudentScores窗体(我单击StudentScores窗体上的"添加新"按钮,AddNewStudent窗体弹出…我输入我的值…然后按"确定"按钮…希望在StudentScore列表框中显示值)。然而,我还不完全熟悉使用类和使用多表单程序,所以我不仅工作速度比平时慢,而且很难弄清楚这一点。非常感谢您的帮助。
我已经有三个初始化的名称和分数,它们用一个|管道字符分隔。我试图在StudentScores列表框中以相同的格式显示任何用户输入。我希望默认情况下有这三个名字和分数。谢谢你抽出时间。
我的学生班:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace My_Program
{
public class Student
{
public Student()
{
this.Scores = new List<int>();
}
public Student (string Name, List<int> Scores)
{ this.Name = Name;
this.Scores = Scores;
}
public string Name
{ get;
set;
}
public List<int> Scores
{ get;
set;
}
public override string ToString()
{
string names= this.Name;
foreach (int myScore in Scores)
{ names += "|" + myScore.ToString();
}
return names;
}
public int GetscoreTotal()
{ int sum = 0;
foreach (int score in Scores)
{
sum += score;
}
return sum;
}
public int GetScoreCount()
{ return Scores.Count;
}
public void addScore(int Score)
{
Scores.Add(Score);
}
}
}
我的主要表格AKA学生成绩表格
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace My_Program
{
public partial class StudentScores : Form
{
//list of students declared
public List<Student> studentList = new List<Student>();
List<Student> Students;
public StudentScores()
{
InitializeComponent();
// listBoxStudents.DataSource = Students;
}
private void StudentScore_Load(object sender, EventArgs e)
{
//initialized 3 students for list box upon loading program...
Students = new List<Student>();
Student student1 = new Student();
student1.Name = "George Mendoza";
student1.Scores.Add(97);
student1.Scores.Add(71);
student1.Scores.Add(83);
Students.Add(student1);
listBoxStudents.Items.Add(student1);
Student student2 = new Student();
student2.Name = "John Doe";
student2.Scores.Add(99);
student2.Scores.Add(93);
student2.Scores.Add(97);
Students.Add(student2);
listBoxStudents.Items.Add(student2);
Student student3 = new Student();
student3.Name = "Bill Cruz";
student3.Scores.Add(100);
student3.Scores.Add(100);
student3.Scores.Add(100);
Students.Add(student3);
listBoxStudents.Items.Add(student3);
}
private void btnAddNew_Click(object sender, EventArgs e)
{
//calls form2 aka AddNewStudent form.
AddNewStudent frm = new AddNewStudent();
frm.ShowDialog();
}
private void btnDelete_Click(object sender, EventArgs e)
{
//code something that will delete entries...message box is a start
DialogResult dialog = MessageBox.Show("Are you sure you want to delete your entries?", "Message", MessageBoxButtons.YesNoCancel);
if (dialog == DialogResult.Yes)
{
while (listBoxStudents.SelectedItems.Count > 0)
{
listBoxStudents.Items.Remove(listBoxStudents.SelectedItems[0]);
}
}
}
private void listBoxStudents_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
添加新学生表格(我添加新名字和分数并将其发送到StudentScores表格的表格):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace My_Program
{
public partial class AddNewStudent : Form
{
List<Student> Students;
public AddNewStudent()
{
InitializeComponent();
}
public AddNewStudent (List <Student> Students)
{
this.Students = Students;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAddScore_Click(object sender, EventArgs e)
{
int score = Convert.ToInt32(txtScore.Text);
//...
}
private void btnOk_Click(object sender, EventArgs e)
{
string name = Convert.ToString(txtName.Text);
if (name == string.Empty)
{
MessageBox.Show("Please enter a name before continuing.", "Entry Error.");
}
else
{
//stuck here...
}
}
}
private void btnClearScores_Click(object sender, EventArgs e)
{
//clears both the score and scores text box
txtScore.Text = "";
txtScores.Text = "";
txtScore.Focus();
}
}
}
您应该;
-
将
StudentScores
中listBoxStudents
的访问修饰符更改为public
-
实现
AddNewStudent
表单的构造函数以接受StudentScores
实例。将StudentScores
实例存储在字段中。private StudentScores _studentScoresForm; public AddNewStudent(StudentScores studentScoresForm) : this() { _studentScoresForm = studentScoresForm; } public AddNewStudent() { InitializeComponent(); }
-
通过传递
this
创建AddNewStudent
表单的实例private void btnAddNew_Click(object sender, EventArgs e) { //calls form2 aka AddNewStudent form. AddNewStudent frm = new AddNewStudent(this); frm.ShowDialog(); }
4.在AddNewStudent
表格中,当发生更改(添加新学生)并单击"确定"按钮时:
Student student1 = new Student();
student1.Name = name;
student1.Scores.Add(score);
_studentScoresForm.listBoxStudents.Add(student1);
如@Oguz所述,您希望将StudentScores
表单作为变量传递给AddNewStudent
表单。
这将允许您从AddNewStudent
表单访问StudentScores
表单中的任何公共属性。换句话说,您可以访问原始学生列表并向其中添加新学生。只传递StudentScores
表单的变量引用,因此所有更改都会反映在原始学生列表中。
在AddNewStudent表单中,此构造函数永远不会被调用:
public AddNewStudent (List <Student> Students)
{
this.Students = Students;
}
所以我认为你想做的是使用它来改变这个:
private void btnAddNew_Click(object sender, EventArgs e)
{
//calls form2 aka AddNewStudent form.
AddNewStudent frm = new AddNewStudent();
frm.ShowDialog();
}
到此:
private void btnAddNew_Click(object sender, EventArgs e)
{
//calls form2 aka AddNewStudent form.
AddNewStudent frm = new AddNewStudent(Students);
frm.ShowDialog();
}
现在,您可以将学生列表传递到表格中,在那里您可以对其进行更改。
最专业的方法是使用委托
在AddNewstudent表单中创建一个具有所需的四个参数的代表
public delegate void PassControl(string a,int b,int c,int d);
public PassControl passControl;
点击AddNewStudent表单中的OK按钮
private void button1_Click(object sender, EventArgs e)
{
if (passControl != null)
{
passControl(txtName.Text,int.Parse(txt1.Text),int.Parse(txt2.Text),int.Parse(txt3.Text));
}
}
在学生成绩表中点击添加新按钮
private void btnAddNew_Click(object sender, EventArgs e)
{
//calls form2 aka AddNewStudent form.
AddNewStudent frm = new AddNewStudent();
frm.passControl = new C2.PassControl(this.PassData);
frm.ShowDialog();
}
在学生成绩表中写一个它所指向的委托函数。
private void PassData(string a,int b,int c,int d)
{
Student student1 = new Student();
student1.Name = a;
student1.Scores.Add(b);
student1.Scores.Add(c);
student1.Scores.Add(d);
Students.Add(student1);
listBoxStudents.Items.Add(student1);
}