子窗体向父窗体传值以及父窗体向子窗体传值

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();
        }

转载于:https://www.cnblogs.com/cxfcxf8/archive/2008/05/01/1179070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值