窗口之间即时控制

打开子窗体Form2后,在往Form2上的控件上输入值的时候,Form1上的控件上的值也跟着变化。

 

Form1代码:

public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TextBoxChanged += new EventHandler(
(sender1, e1) =>
{ textBox2.Text = f2.TextBoxValue; }
);
f2.FormClosed += new FormClosedEventHandler(
(sender2, e2) => { f2 = null; }
);
f2.Show(this);
}

 

 

Form2代码:

 

public Form2():this(null)
{

}

public string TextBoxValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

public event EventHandler TextBoxChanged;

public Form2(string value) {
InitializeComponent();
TextBoxValue = value;
}

private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (TextBoxChanged != null)
{
TextBoxChanged(this, e);
}
}

posted on 2017-07-13 16:36  homie  阅读(129)  评论(0编辑  收藏  举报