【发布时间】:2015-11-04 08:53:14
【问题描述】:
我正在尝试将用户输入从我的 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);
}
}
}
我的主要表格又名 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();
}
}
}
【问题讨论】: