利用委托轻松实现,子窗体向父窗体传值。

子窗体实现代码:

//声明委托
public delegate void MyDelMsg(string msg);
//定义一个委托变量
public  MyDelMsg myDelMsg;

private void button1_Click(object sender, EventArgs e)
{
       if (myDelMsg!=null)
       {
             //调用
             myDelMsg(textBox1.Text);
             this.Close();
        }
}

 

父窗体调用子窗体

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(textBox1.Text);
            frm.myDelMsg = new Form2.MyDelMsg(RMsg);
            frm.ShowDialog();
        }

        private void RMsg(string msg)
        {
            textBox1.Text = msg;
        }    

 

 

posted on 2014-03-07 13:51  Y.G.H  阅读(519)  评论(0编辑  收藏  举报