C#窗体间常用的几种传值方式、以及委托与事件的详细介绍
窗体间的传值,最好使用委托方式传值,开始之前,我们先来说一下委托与事件的关系。
委托:是一个类。
事件:是委托类型的一个特殊实例,只能在类的内部触发执行。
首先创建2个窗体,这里我们以form1为发送窗体,form2为接收窗体
form1窗体
form2窗体
方式一(最简单的方式)#
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 事件的方式实现窗体间传值 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Form2 msgFrm { get; set; } private void Form1_Load(object sender, EventArgs e) { Form2 f2 = new Form2(); msgFrm = f2; f2.Show(); } private void btnSendMsg_Click(object sender, EventArgs e) { //对象内部的,字段或者元素属性最好不要直接让外部直接访问 //最好是通过,设置的方法来控制一下 msgFrm.SetTxt(this.txtMsg.Text); } } }
form2窗体代码
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 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void SetTxt(string txt) 20 { 21 this.txtMsg.Text = txt; 22 } 23 } 24 }
方式二(委托方式)#
注:委托不熟悉的宝宝们,请自行查阅Func与Action,以及delegate三者区别,这里我们用系统内置的委托Action
form1窗体代码
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 12 namespace 事件的方式实现窗体间传值 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 //定义委托 21 public Action<string> afterMsgSend { get; set; } 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 Form2 f2 = new Form2(); 25 afterMsgSend += f2.SetTxt; //给系统内置的委托注册事件 26 f2.Show(); 27 } 28 29 private void btnSendMsg_Click(object sender, EventArgs e) 30 { 31 if (afterMsgSend == null) 32 { 33 return; 34 } 35 afterMsgSend(this.txtMsg.Text); 36 } 37 } 38 }
form2窗体代码
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 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void SetTxt(string txt) 20 { 21 this.txtMsg.Text = txt; 22 } 23 } 24 }
方式三(事件方式,更安全哟)#
TextBoxMsgChangeEventArg类继承EventArgs代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 事件的方式实现窗体间传值 8 { 9 public class TextBoxMsgChangeEventArg:EventArgs 10 { 11 public string Text { get; set; } 12 } 13 }
form1窗体代码
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 12 namespace 事件的方式实现窗体间传值 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 public event EventHandler AfterMsgChange; 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 Form2 f2 = new Form2(); 24 AfterMsgChange += f2.AfterTxtChange; 25 f2.Show(); 26 } 27 private void btnSendMsg_Click(object sender, EventArgs e) 28 { 29 AfterMsgChange(this, new TextBoxMsgChangeEventArg() { Text = this.txtMsg.Text }); 30 } 31 } 32 }
form2窗体
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 事件的方式实现窗体间传值 12 { 13 public partial class Form2 : Form 14 { 15 public Form2() 16 { 17 InitializeComponent(); 18 } 19 public void AfterTxtChange(object sender,EventArgs e) 20 { 21 //拿到父窗体传来的文本,强转数据类型 22 TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg; 23 this.SetTxt(arg.Text); 24 } 25 } 26 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?