在我的学生注册表中,我在主表单中有一个列表框,在我的第二个表单中我添加了学生。在第二种形式中,我在名称文本框中添加一个名称,并为学生添加需要用竖线字符(“|”)分隔的数字。在用户按下okay按钮后填写文本框中的名称和数字后,它应该看起来像下面的硬编码学生。代码
第一表格
private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = listForm1.GetItemText(listForm1.SelectedItem);
string[] valArray = val.Split('|');
int sum = 0;
int scores = 0;
for (int i = 1; i < valArray.Length; i++)
{
int num = Convert.ToInt32(valArray[i]);
sum += num;
scores++;
}
int average = sum / scores;
txtAverage.Text = average.ToString();
txtTotal.Text = sum.ToString();
txtScoreCount.Text = scores.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
listForm1.Items.Add("Hamish Overtone" + "|" + 39 + "|" + 12 + "|" + 85);
listForm1.Items.Add("Claudia Dye" + "|" + 44 + "|" + 56 + "|" + 85);
listForm1.Items.Add("Mike Layne" + "|" + 12 + "|" + 47+ "|" + 28);
}
第二表格
private void btnADDSCORE_Click(object sender, EventArgs e)
{
string userInput = txtNewScore.Text;
txtNewScores.Text = txtNewScores.Text + " " + userInput;
txtNewScore.Text = String.Empty;
}
private void btnNewOk_Click(object sender, EventArgs e)
{
}
private void txtNewName_TextChanged(object sender, EventArgs e)
{
}
}
ERRORING PART:
private void listForm1_SelectedIndexChanged(object sender,EventArgs e) {
// Get the value from the selected item
string val = listForm1.GetItemText(listForm1.SelectedItem);
// Split the item's value to a string array according to the pipe char
string[] valArray = val.Split('|');
int sum = 0;
int scores = 0;
// Iterate through all possible values and sum it up,
// while keeping count to how many numbers there are:
for (int i = 1; i < valArray.Length; i++)
{
int num = Convert.ToInt32(valArray[i]);
sum += num;
scores++;
}
// Calculate the average.
// Keep in mind using an integer will create a whole number, without decimal points.
int average = sum / scores;
// Place the average and the sum in textboxes
txtAverage.Text = average.ToString();
txtTotal.Text = sum.ToString();
txtScoreCount.Text = scores.ToString();
}
答案 0 :(得分:1)
在主窗口(此处为Form1)中,您需要创建一个委托,允许您传递数据,例如字符串。然后,您创建该委托类型的实例并订阅匹配的方法。然后,当您打开辅助窗口时,将该委托传递给辅助窗口。
public delegate void DataTransfer(string data);
public partial class Form1 : Form
{
public DataTransfer transferDelegate;
public Form1()
{
InitializeComponent();
transferDelegate += new DataTransfer(DataMethod);
}
public void DataMethod(string data)
{
// Do what you want with your data.
MessageBox.Show(data);
}
private void button1_Click(object sender, EventArgs e)
{
InputWindow win = new InputWindow(transferDelegate);
win.Show();
}
}
现在,当您在辅助窗口中调用该委托时,将调用Form1的DataMethod()
,因此您可以在窗口之间传递信息。
您的辅助窗口实现应如下所示:
public partial class InputWindow : Form
{
DataTransfer transferDel;
public InputWindow(DataTransfer del)
{
InitializeComponent();
transferDel = del;
}
private void button1_Click(object sender, EventArgs e)
{
string data = textBox1.Text;
transferDel.Invoke(data);
}
}
如您所见,当您调用传递的委托时,它会调用主程序中Form1的相应方法。
答案 1 :(得分:0)
尝试添加按钮并使用以下代码(在form1类中)到其click事件处理程序。
class form1
{
private void form1_addButton_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.ShowDialog();
foreach(string s in newForm.studentInfo)
{
listForm1.Items.Add(s);
}
}
}
以下代码将添加到您的form2类文件中。
class form2
{
public List<string> studentInfo {get; set;} //add this to form2
private void form2_addButton_Click(object sender, EventArgs e)
{
//add scores to list here.
}
}
修改
//THIS IS IN FORM2 :)
private void btnNewOk_Click(object sender, EventArgs e)
{
string student = textBoxName.Text;
string[] scores = textBoxNewScores.Text.Trim().Split(' ');
for(int i = 0; i < scores.Length; i++)
{
student = student + "|" + scores[i];
}
studentInfo.Add(student);
this.Close();
}
修改2
private void listForm1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = listForm1.GetItemText(listForm1.SelectedItem);
string[] valArray = val.Split('|');
int sum = 0;
int scores = 0;
for (int i = 1; i < valArray.Length; i++)
{
int num = Convert.ToInt32(valArray[i]);
sum += num;
scores++;
}
int average = 0;
if (scores != 0)
average = sum / scores;
txtAverage.Text = average.ToString();
txtTotal.Text = sum.ToString();
txtScoreCount.Text = scores.ToString();
}