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

被折叠的 条评论
为什么被折叠?



