我有一个名为“ studentInfo”的列表,我试图允许用户转到我创建的名为“ addRecord”的表单,并输入学生代码和学生标记,一旦他们点击提交,便会添加数据到form1的列表中,然后数据也被添加到form1的列表框中。
由于我是C#的新手,所以我不知道该怎么做,因此我们将不胜感激。
我的列表是这样创建的
request()->headers->get('referer')
然后,在我的form1加载后,我从CSV文件中读取内容以填充列表框
public static List<string> studentInfo = new List<string>();
我希望用户转到addRecord表单,输入学生代码和学生标记,然后单击“提交”,然后程序要求用户确认他们输入的数据正确,然后将用户定向回去。到form1,数据将在列表框中。
答案 0 :(得分:0)
好的,在Form addRecord中,我们创建两个属性来保存要从主Form中检索的数据。仅当addRecord返回DialogResult.OK时才应这样做。我添加了两个按钮,一个按钮用于取消,另一个按钮用于确定。这些只是将DialogResult设置为其各自的结果。还可以在“确定”按钮处理程序中设置要返回的两个值的属性值:
public partial class addRecord : Form
{
public addRecord()
{
InitializeComponent();
}
private string _Student;
public string Student
{
get
{
return this._Student;
}
}
private string _Score;
public string Score
{
get
{
return this._Score;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
this._Student = this.tbStudent.Text;
this._Score = this.tbScore.Text;
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
现在,回到Form1,这是一个使用ShowDialog()显示addRecord,然后在返回OK时检索存储的值的示例:
public partial class Form1 : Form
{
public static List<string> studentInfo = new List<string>();
// ... other code ...
private void btnAddRecord_Click(object sender, EventArgs e)
{
addRecord frmAddRecord = new addRecord();
if (frmAddRecord.ShowDialog() == DialogResult.OK)
{
studentInfo.Add(frmAddRecord.Student);
studentInfo.Add(frmAddRecord.Score);
lstMarks.Items.Add(frmAddRecord.Student + " : " + frmAddRecord.Score);
}
}
}
---编辑---
这是addRecord的不同版本,在输入有效名称和有效分数之前,不允许您单击OK。请注意,该属性已更改为int,并且TextChanged()事件已连接到ValidateFields()。最初还禁用了“确定”按钮:
public partial class addRecord : Form
{
public addRecord()
{
InitializeComponent();
this.tbStudent.TextChanged += ValidateFields;
this.tbScore.TextChanged += ValidateFields;
btnOK.Enabled = false;
}
private string _Student;
public string Student
{
get
{
return this._Student;
}
}
private int _Score;
public int Score
{
get
{
return this._Score;
}
}
private void ValidateFields(object sender, EventArgs e)
{
bool valid = false; // assume invalid until proven otherwise
// Make sure we have a non-blank name, and a valid mark between 0 and 100:
if (this.tbStudent.Text.Trim().Length > 0)
{
int mark;
if (int.TryParse(this.tbScore.Text, out mark))
{
if (mark >= 0 && mark <= 100)
{
this._Student = this.tbStudent.Text.Trim();
this._Score = mark;
valid = true; // it's all good!
}
}
}
this.btnOK.Enabled = valid;
}
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}