我正在尝试将用户输入从 AddNewStudent 表单传递到我的主 StudentScores 表单(我单击StudentScores表单上的btnAddNew按钮,弹出AddNewStudent表单.. 。我输入我的值...然后按btnOk按钮...希望将值显示在StudentScores列表框中)。但是,我还不熟悉使用类和使用多表单程序,因此我不仅比平常工作慢,而且我一直很难搞清楚这一点。非常感谢任何帮助。
我已经有三个初始化的名称和分数由|分隔管道人物。我试图在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 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 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();
}
}
}
AddNewStudent 表单(我添加新名称和分数并将其发送到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();
}
}
}
答案 0 :(得分:0)
你应该;
将listBoxStudents
中的StudentScores
的访问修饰符更改为public
在AddNewStudent
表单中实现构造函数以接受StudentScores
实例。将StudentScores
实例存储在字段中。
private StudentScores _studentScoresForm;
public AddNewStudent(StudentScores studentScoresForm) : this()
{
_studentScoresForm = studentScoresForm;
}
public AddNewStudent()
{
InitializeComponent();
}
通过传递AddNewStudent
this
表单的实例
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);
答案 1 :(得分:0)
正如 @Oguz 已经解释过,您希望将StudentScores
表单作为变量传递给AddNewStudent
表单。
这样,您就可以从StudentScores
表单访问AddNewStudent
表单中的任何公共属性。换句话说,您可以访问原始学生列表并添加新学生。只传递StudentScores
表单的变量引用,以便所有更改都反映在原始学生列表中。
答案 2 :(得分:0)
在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();
}
现在,您将学生列表传递到可以对其进行更改的表单。
答案 3 :(得分:0)
最专业的方法是使用代表
在AddNewstudent表单中,创建一个包含四个必需参数的委托
public delegate void PassControl(string a,int b,int c,int d);
public PassControl passControl;
单击AddNewStudent表单中的确定按钮
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);
}