c# 2个form的textbox同步显示

来源:百度知道 编辑:UC知道 时间:2024/06/08 18:46:55
怎样让2个Form里的textbox同步输入,在form1的textbox输入时,同时在form2的textbox里显示?

为Form2增添一个属性,公开textBox2,并Form1中实例化Form2时,记录为字段。
Form2中:
public TextBox TextBox2
{
get { return textBox2; }
set { textBox2 = value; }
}
Form1中:
Form2 frm2;
private void button_Click(object sender,EventArgs e)//假设用此按钮单击事件委托打开Form2(即实例化Form2)
{
frm2=new Form2(this);
frm2.Show();
}
然后在Form1的textBox1的TextChanged事件委托函数中写:
frm2.Text=textBox1.Text;

private void textBox2_TextChanged(object sender, EventArgs e)
{
f1.textBox1.Text = textBox2.Text;
}
将form1中的textBox1的Modifiers 设置为Public
给textBox2添加一个textBox2_TextChanged事件,f1为form1的实例