MDI子窗口+事件与委托的一个例程
1首先,新建WinForm的.NetFramWork的工程并添加2个Form:
2设置 Form1为MDI主窗口;
【属性】——
将以上属性改为 True;
另外,也可以采用代码形式:
this.IsMdiContainer = true;//设置容器属性
来实现同样的功能,此Demo采用代码来实现;
3设置Form2为Form1的子窗口;
在Form1的Load事件中添加如下代码:
private void Form1_Load(object sender, EventArgs e) { frm2.MdiParent = this; frm2.StartPosition = FormStartPosition.CenterScreen;//设置子窗体在父窗体中间显示 frm2.Show(); }
至此,即可实现MDI多窗口编程;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接下来,运用之前学习的委托与事件来实现一些功能;
【1】在弹出的Form2的上面自动生成2个控件,一个Textbox,一个Button;在Form2实现的关键代码如下:
public Form2() { InitializeComponent(); button1.Top = 50; button1.Text = "Say Hello"; this.Controls.Add(button1); this.Controls.Add(textbox1);
【2】点击Button“Say Hello”会让Button的标题显示到Textbox上;此功能输入事件模型中的——自己的成员事件被自己的成员事件处理器来订阅这种;关键代码如下:
>>事件订阅:
public Form2() { button1.Click += this.Action1;
>>另外一部分——事件处理器 方法:
private void Action1(object sender, EventArgs e) { this.textbox1.Text = this.button1.Text;
【3】点击Button“Say Hello”会让Form1窗口的标题栏事件发生更新;
由于Form1和Form2不在同一个Class;实现此功能仍然第一个想到是事件,但是,事件处理器(方法)里无法直接操作Form的属性;于是自然地想到了委托;
第一步:创建委托:注意——委托实际是一种特殊的类,所以委托的创建(定义)一定记得一般在NameSpace下,与Class同一级别;代码如下:
namespace ParentFormDemo { public delegate void ActionDelegate(object sender, EventArgs e);//【1】定义委托 public partial class Form1 : Form {
第二步:在Form2中定义委托变量(因为事件实在Form2中发生的,必须顶一个委托变量作为Form2的Public字段成员,然后再在Form1中访问,即可实现绑定Form1中的方法):
public partial class Form2 : Form { public ActionDelegate actionDelegate1 = null;//【2】定义委托变量
在Form1中绑定定义满足签名的方法;
private void Form1_Load(object sender, EventArgs e) { frm2.actionDelegate1 = ChangeTitle;//【3】绑定委托 frm2.MdiParent = this; frm2.StartPosition = FormStartPosition.CenterScreen;//设置子窗体在父窗体中间显示 frm2.Show(); }
满足签名的方法:
public void ChangeTitle(object sender, EventArgs e) { this.Text = DateTime.Now.ToString("HH:mm:ss .fff") + ">>子窗口的按钮被按下"; }
再同上一个功能是实现,事件绑定该方法,此处由于都是同一个Button click事件的响应,故只需在同一个事件处理器(方法)中添加对委托的调用即可;
private void Action1(object sender, EventArgs e) { this.textbox1.Text = this.button1.Text; actionDelegate1(null,null);//【5】调用委托 }
至此完成了功能。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
其他实现了一些类似的自由控制显示等等小功能,都是通过事件和委托来实现的;所以说,C#学会并深入理解了2大神技:事件与委托,并能活学活用,C#编程技术才能说是刚刚入门;
最后,贴上完整的代码运行截图:
效果图:
源代码:
Form1:

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 ParentFormDemo { public delegate void ActionDelegate(object sender, EventArgs e);//【1】定义委托 public partial class Form1 : Form { Label label1 = new Label();//Form1的字段成员变量,要放在Class里面 Form2 frm2 = new Form2(); public Form1() { InitializeComponent(); this.IsMdiContainer = true;//设置容器属性 this.Controls.Add(label1); label1.Size =new Size(this.Size.Width, 50); label1.BackColor = Color.Yellow; label1.Text ="第1步:演示了一个子窗口依附在父窗口上,这种MDI窗口模式"+ "\r\n" + "第2步:请按下按钮,观察其上方的显示框内容会显示按钮名称" + "\r\n"+"第3步,在观察父窗口的标题栏变化"; this.Resize += Form1_Resizing; this.ResizeEnd+= Form_ResizeEnd; } private void Form_ResizeEnd(object sender, EventArgs e) { frm2.textbox1.Text = "完成";//在子窗口显示信息 frm2.textbox1.BackColor = Color.White; } public void Form1_Resizing(object sender, EventArgs e) { frm2.textbox1.Text = "正在拖拽改变主窗口大小";//在子窗口显示信息 label1.Size = new Size(this.Size.Width, 50); frm2.textbox1.BackColor = Color.Yellow; } private void Form1_Load(object sender, EventArgs e) { frm2.actionDelegate1 = ChangeTitle;//【3】绑定委托 frm2.MdiParent = this; frm2.StartPosition = FormStartPosition.CenterScreen;//设置子窗体在父窗体中间显示 frm2.Show(); } public void ChangeTitle(object sender, EventArgs e) { this.Text = DateTime.Now.ToString("HH:mm:ss .fff") + ">>子窗口的按钮被按下"; } } }
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 ParentFormDemo { public partial class Form2 : Form { Button button1 = new Button(); public TextBox textbox1 = new TextBox(); public ActionDelegate actionDelegate1 = null;//【2】定义委托变量 public Form2() { InitializeComponent(); button1.Top = 50; button1.Text = "Say Hello"; this.Controls.Add(button1); this.Controls.Add(textbox1); textbox1.Size = new Size( this.Width,50); button1.Click += this.Action1; this.Resize += this.SiziChanging; this.ResizeEnd += this.SizeChangedEnd; } private void SizeChangedEnd(object sender, EventArgs e) { this.textbox1.Text = "子窗口大小改变完成"; this.textbox1.BackColor = Color.White; } private void SiziChanging(object sender, EventArgs e) { this.textbox1.BackColor = Color.Yellow; this.textbox1.Size = new Size(this.Size.Width, 50); this.textbox1.Text = "正在拖拽子窗口大小"; } private void Action1(object sender, EventArgs e) { this.textbox1.Text = this.button1.Text; actionDelegate1(null,null);//【5】调用委托 } } }
Main(程序入口):

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace ParentFormDemo { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
大家感兴趣的小伙伴们可以,copy到自己的工程试试啦
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?