1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApplication1 12 { 13 public partial class Form1 : Form 14 { 15 private string name; 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 Form2 frm2 = new Form2(); 24 frm2.MdiParent = this; 25 frm2.Show(); 26 } 27 28 private void button2_Click(object sender, EventArgs e) 29 { 30 Form3 frm1 = new Form3(); 31 frm1.MdiParent = this; 32 frm1.Show(); 33 } 34 public event Action<string> OnText; 35 public void ToChangeText(string obj) 36 { 37 if (OnText != null) 38 { 39 OnText(obj);} 40 } 41 private void Form1_Load(object sender, EventArgs e) 42 { 43 44 } 45 } 46 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApplication1 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 (this.ParentForm as Form1).ToChangeText("form2来的消息啊?"); 23 } 24 } 25 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsApplication1 12 { 13 public partial class Form3 : Form 14 { 15 public Form3() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form3_Load(object sender, EventArgs e) 21 { 22 MessageBox.Show(this.ParentForm.Name); 23 (this.ParentForm as Form1).OnText += new Action<string>(Form3_OnText); 24 } 25 private void Form3_FormClosed(object sender, FormClosedEventArgs e) 26 { 27 (this.ParentForm as Form1).OnText -= new Action<string>(Form3_OnText); 28 } 29 void Form3_OnText(string obj) 30 { 31 textBox1.Text = textBox1.Text+obj; 32 } 33 } 34 }
时间: 2024-10-06 15:17:21