WinForm中子父窗体传值

子窗体向父窗体传值以及父窗体向子窗体传值
Code
1: 所有权法
//Form1:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//
}
//在调用Form2时,要把Form2的所有者设为Form1
Form2 f2 = new Form2() ;
f2.Owner = this;
f2.ShowDialog() ;
//Form2:
//在需要对其调用者(父)刷新时
Form1 f1 ;
f1 = (Form1)this.Owner;
f1.Refresh_Method() ;

eg:

Form1中的函数:

public void DiaoYong(string str)
{
this.textBox1.Text =str;
}

private void button2_Click(object sender, EventArgs e)
{

string str = this.textBox1.Text;
Form2 f2 = new Form2(str);//在构造函数中,向子窗体传值。
f2.Owner = this;
f2.ShowDialog();
}

Form2中的函数:

public Form2(string ss)
{
InitializeComponent();
this.textBox1.Text = ss;

}

private void button1_Click(object sender, EventArgs e)
{
string st = textBox1.Text;

Form1 f1;
f1 = (Form1)this.Owner;
f1.DiaoYong(st);

this.Close();
}

你可能感兴趣的:(WinForm)