码迷,mamicode.com
首页 > Windows程序 > 详细

Window Form 父子窗体相互更新

时间:2018-06-23 13:16:19      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:+=   rtp   ges   str   子窗体   initial   parent   enter   screen   

主窗体

public partial class Form1 : Form
{
public Form1()
{
//LoginForm dlg = new LoginForm();
//dlg.StartPosition = FormStartPosition.CenterParent;
//dlg.ShowDialog();
InitializeComponent();
//this.StartPosition = FormStartPosition.CenterScreen;
}

//父窗体定义委托和事件
public delegate void changetxt(string text);
public event changetxt changeStxt_event;
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);//传递窗体1指针
//子窗体订阅事件
frm.changeFtext_event += new Form2.changetext(frm_changetext_event);
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Show(this);//窗体不会置于父窗体的外边
}

void frm_changetext_event(string text)
{
textBox1.Text = text;
}

private void button2_Click(object sender, EventArgs e)
{
changeStxt_event(textBox1.Text);
}
}

子窗体

public partial class Form2 : Form

{
public Form2(Form1 frm)
{
InitializeComponent();
//订阅事件
frm.changeStxt_event += new Form1.changetxt(frm_changeStxt_event);
}

//子窗体定义委托事件
public delegate void changetext(string text);
public event changetext changeFtext_event;
//更新方法
void frm_changeStxt_event(string text)
{
textBox1.Text = text;
}

private void button1_Click(object sender, EventArgs e)
{
changeFtext_event(textBox1.Text);
}
}

 

Window Form 父子窗体相互更新

标签:+=   rtp   ges   str   子窗体   initial   parent   enter   screen   

原文地址:https://www.cnblogs.com/hbgjh/p/9216626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!