博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# MDI子窗体互相操作
阅读量:4612 次
发布时间:2019-06-09

本文共 2748 字,大约阅读时间需要 9 分钟。

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 WindowsFormsApplication112 {13     public partial class Form1 : Form14     {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
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 WindowsFormsApplication112 {13     public partial class Form2 : Form14     {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 WindowsFormsApplication112 {13     public partial class Form3 : Form14     {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
(Form3_OnText);24 }25 private void Form3_FormClosed(object sender, FormClosedEventArgs e)26 {27 (this.ParentForm as Form1).OnText -= new Action
(Form3_OnText);28 }29 void Form3_OnText(string obj)30 {31 textBox1.Text = textBox1.Text+obj;32 }33 }34 }

 

转载于:https://www.cnblogs.com/zhutiehan/p/5720403.html

你可能感兴趣的文章