
正文
C# MDI子窗体互相操作
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private string name; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.MdiParent = this; frm2.Show(); } private void button2_Click(object sender, EventArgs e) { Form3 frm1 = new Form3(); frm1.MdiParent = this; frm1.Show(); } public event Action<string> OnText; public void ToChangeText(string obj) { if (OnText != null) { OnText(obj);} } private void Form1_Load(object sender, EventArgs e) { } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { (this.ParentForm as Form1).ToChangeText("form2来的消息啊?"); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void Form3_Load(object sender, EventArgs e) { MessageBox.Show(this.ParentForm.Name); (this.ParentForm as Form1).OnText += new Action<string>(Form3_OnText); } private void Form3_FormClosed(object sender, FormClosedEventArgs e) { (this.ParentForm as Form1).OnText -= new Action<string>(Form3_OnText); } void Form3_OnText(string obj) { textBox1.Text = textBox1.Text+obj; } } }







