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

本文介绍两种在Windows Form应用中实现窗体间数据传递的方法:使用Action委托和事件。通过实例展示了如何从Form1向Form2传递字符串参数,并在Form2中接收并显示这些参数。

这里使用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;
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值