C#两窗体互相通讯员

来源:百度知道 编辑:UC知道 时间:2024/06/16 04:48:32
哪们大侠能帮忙做一个WinForm程序,要求:
主窗口(Form1)调用一个无模式对话框(Form2),在两个窗体中皆有一个textBox,在Form2中的textBox中输入字符时,Form1中的textBox中出现相同的字符,不能用Timer控件!!!一定要用事件。类似于
private void textBox2_TextChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox2.Text;
}

辛苦了,如果程序很复杂,可以再加分!

使用委托事件。
代码如下
第一个窗体Form4:
namespace WinFormTest
{
public delegate void TestDelegate(string message);
public partial class Form4 : Form
{
public static TestDelegate testDelegate = null;
public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)
{
testDelegate = new TestDelegate(ShowMessage);
}

private void button1_Click(object sender, EventArgs e)
{
Form5 form5 = new Form5();
form5.Show();
}

private void ShowMessage(string message)
{
this.textBox2.Text = message;
}
}
}

第二个窗体Form5:
namespace WinFormTest
{
public partial class Form5 : Form
{
public Form5()
{