如何从另一个表单传递数据并返回
How to pass data from another form and back
我一直在努力让它工作。我确定我写对了
表格 1
private string _temp;
public string Temp
{
get { return _temp; }
set { _temp = value; }
}
private void button1_Click(object sender, EventArgs e)
{
_temp = "test";
}
表格 2
private void button1_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
MessageBox.Show(main.Temp);
}
您必须通过 sender
参数获取对 Form1
实例的引用(假设 Form2
的 button1_Click
处理程序附加到 Form1
):
//Form2
private void button1_Click(object sender, EventArgs e)
{
Button button = (Button) sender;
Form1 main = (Form1) button.FindForm();
MessageBox.Show(main.Temp);
}
我一直在努力让它工作。我确定我写对了
表格 1
private string _temp;
public string Temp
{
get { return _temp; }
set { _temp = value; }
}
private void button1_Click(object sender, EventArgs e)
{
_temp = "test";
}
表格 2
private void button1_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
MessageBox.Show(main.Temp);
}
您必须通过 sender
参数获取对 Form1
实例的引用(假设 Form2
的 button1_Click
处理程序附加到 Form1
):
//Form2
private void button1_Click(object sender, EventArgs e)
{
Button button = (Button) sender;
Form1 main = (Form1) button.FindForm();
MessageBox.Show(main.Temp);
}