委托和事件(四)——简单传值

这里使用Action,直接传值,

功能是:form1打开form2时,把值传过去

Form1

Form2

 

1 使用委托:

Form1代码:

private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            f2.act1(textBox1.Text,textBox2.Text);
        }

Form2代码:

public Action<string, string> act1;
        private void Form2_Load(object sender, EventArgs e)
        {
            act1 = (x, y) => {
                textBox1.Text = x;
                textBox2.Text = y;
            };
        }

 

 

2 使用事件

Form1

public event Action<string, string> act1;
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            act1 = f2.Test;
            act1(textBox1.Text,textBox2.Text);
        }

 

Form2

public void Test(string x, string y)
        {
            textBox1.Text = x;
            textBox2.Text = y;
        }

 

posted @ 2019-04-30 15:27  正怒月神  阅读(218)  评论(0)    收藏  举报