1.form2传值给form1
form1
textbox1, button1
form2
button1
form1:
private void button1_Click(object sender, EventArgs e)
{
value.Form2 form2 = new value.Form2();
form2.ReturnValue = new value.Form2.returnvalue(showvalue);
form2.ShowDialog();//
form2.Show();
}
private void showvalue( int i)
{
this.textBox1.Text = i.ToString();
}
form2:
public delegate void returnvalue(int i);
public returnvalue ReturnValue;
private void button1_Click(object sender, EventArgs e)
{
if (ReturnValue != null)
{
ReturnValue(99);
}
}
2.form1传值给form2
form1
button1
form2
button1,textbox1
form1:
private void button1_Click(object sender, EventArgs e)
{
value.Form2 form2 = new value.Form2("hello!");
form2.Show();
}
form2:
public Form2(string str)
{
InitializeComponent();
this.textBox1.Text = str;
}
3.form1与form2之间的双向传值
(form1与form2可同时改变静态变量的值)
form1:
public static string str = "hello!";
form2:
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = Form1.str;
}