winform 父窗体与子窗体数据传递
简单实例截图:
父窗体代码如下:
public partial class Form1 : Form
{
Child2 child2 = new Child2();
public Form1()
{
InitializeComponent();
child2.receive += new Child2.receiveData(testReceive);
}
private void button2_Click(object sender, EventArgs e)
{
string ls = textBox1.Text;
for(int i=0;i< child2.Controls.Count; i++)
{
Control c = child2.Controls[i];
if (c.Name == "txtReceive")
{
c.Text = "通过查找控件传递数据:"+ls;
}
}
child2.getReceiveData("通过函数传递数据:"+ls);
}
private void button3_Click(object sender, EventArgs e)
{
child2.Show();
}
private void testReceive(object o)
{
textBox2.Text = o.ToString();
}
}
子窗体代码如下:
public partial class Child2 : Form
{
public delegate void receiveData(object o);
public event receiveData receive;
public Child2()
{
InitializeComponent();
}
public void getReceiveData(object o)
{
txtReceive2.Text = o.ToString();
}
public void Receive(object o)
{
receive(o);
}
private void button1_Click(object sender, EventArgs e)
{
Receive(txtReceive3.Text);
}
}
一个简单的父子窗体数据传递。亲测,可以实现上面的效果,谢谢!