当前位置:网站首页>C# 利用委托事件进行窗体间的传值
C# 利用委托事件进行窗体间的传值
2022-04-22 00:08:00 【刘欢(C#)】
引言:
窗体间传值是每个学习WinForm新手的常见问题,最初级的方法就是 在窗体中先获取到要接受值窗体。然后通过.得到某个空间或者属性,直接赋值,这个需要接收放的窗体属性或者空间必须是public ,是极不安全的一种方式。在升级一下的新手做法就是在接受窗体中定义一个方法然后通过 调用这个方法来给某个控件或者属性赋值。这两种方法都是非安全的低级方式。非常不建议使用。本文我们讲讲如何通过委托事件(非单纯的委托)来解决窗体间信息传递的问题。
场景:
通过点击主主窗体From_Main的发送按钮,所有子窗体中显示出发送的内容,如下图

学习前提:
对委托有初步了解。如果你对委托一点不了解,建议先看一下本人前一篇关于委托的基础介绍文章。
正文:
第1步:定一个委托
代码:
public delegate void SendMessageToChildForms(string s); //定义了一个参数是string ,无返回值的委托,名为SendMessageToChildForms。
说明:1.他可以定义在项目中任何可以被访问到的地方(如主窗体,某一个子窗体,或者一个类) 。但原则上定义在一直存在的窗体中,本例在主窗体中的命名空间标题下。
2.系统内置了两个定义好的委托类型 Action 和Func ,你也可以直接省略这一步。在第3步实例化他就可以了。其中Action是无返回值的委托,Func是带返回值的委托。他们的输入参个数数都是0~16个。
第2步:实例化一个此委托类型的事件
代码:
public event SendMessageToChildForms smtcf_event;
说明:1.可以省略event ,public有时也不写,就可以简写成 SendMessageToChildForms smtcf;不利于新手理解,其实他本质就是实例化了一个事件event
2.如果我们使用了微软内置的委托类型Action或者Func 则可以写成这样 public event Action<string> SendMessageToChildForms; 这里string 必须要写,因为没有第1步所以这里需要指明参数列表。
3.委托与委托事件的关系,本例中使用了委托事件 ,相对于委托更安全。更低耦合。委托是一个类型,例如SendMessageToChildForms类,事件是委托类型的一个实例 如smtcf_event。
第3步:定义具体执行的方法
代码:
public void ToShowGetMessage(string s)
{
this.lb_收到内容.Text=s;
}
说明:1.具体的功能方法,这里那个窗体需要执行就写在哪个窗体里,比如本例是更新子窗体的显示,所以写到子窗体里,如果换成另一场景:子窗体中删除了当前这条信息,需要更新,那么就定义到主窗体中
2.方法的参数列表及返回类型必须与委托完全一致。比如本例,参数列表是一个string消息字符串,返回类型是void空,方法必须是public;
第4步:方法绑定到委托上
代码:
Form_Child frm_child = new Form_Child();
smtcf_event+= new SendMessageToChildForms(frm_child.ToShowGetMessage); //在一实例化的一个委托事件上绑定子窗体的具体方法
frm_child.Show();
Form_Child frm_child2 = new Form_Child();
smtcf_event+= new SendMessageToChildForms(frm_child2.ToShowGetMessage); //在一实例化的一个委托事件上绑定子窗体的具体方法
frm_child2.Show();
注意:1.把要执行的方法在新窗体初始化时绑定到他里面定义好的委托类型事件上,必须要在子窗体Show()时间之前
2.一个委托事件smtcf 可以绑定多个方法。
第5步:触发委托
代码:
//委托第5步:触发委托
if (smtcf_event!= null) //判断委托事件是否为空,如果委托不为空才执行
{
smtcf_event.Invoke(this.textBox1.Text.Trim());// 可以省略Invoke 简写为smtcf(this.textBox1.Text.Trim());
}
说明:1.必须首先判断一下委托事件smtcf是否为空。再执行;
2.一般 简写为smtcf_event(this.textBox1.Text.Trim());
3.网上有人说委托事件smtcf_event不能直接调用Invoke()方法。本例中测试可以,有知道原因的这里请教一下各位。
版权声明
本文为[刘欢(C#)]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_36074218/article/details/124326776
边栏推荐
- 续流二极管在开关电源的作用
- Fs2451 can replace the high-voltage DCDC chip of MPS mp2451 to help the design of smart meter 40v0 5A step-down IC
- How about huarongda futures? Is it safe to open futures accounts?
- AI helps to monitor the wearing of labor protection equipment and escort the safe production of enterprises
- 基于apache集合工具包的并集、交集、差集工具类
- Leetcode -- character string
- Analysis of EMI suppression methods of switches and diodes in switching power supply
- 【網絡協議】DHCP和PXE
- Applet subcontracting
- 分享我常用的28个自媒体工具,赶紧收藏,视频2万播放就有264收益
猜你喜欢

2022中国眼博会,北京青少年眼健康产业展会,护眼教育设备展

Kuangshi Research Institute | dark vision network: RGB IR Fusion low illumination imaging method using depth inconsistent a priori

继电器为什么要并联二极管

1.2V Ni MH battery lamp solar lawn lamp boost drive IC silk screen printing aatlb package SOT23-6 and sop8 single chip microcomputer IC

Introduction à la technologie des conteneurs de la série Container Cloud

FANSEA 4W 单线圈发射无线充5W模块

Fs2451 can replace the high-voltage DCDC chip of MPS mp2451 to help the design of smart meter 40v0 5A step-down IC

AVL balanced binary tree and its four rotation modes

隐私计算--36--联邦学习加速方法

Sp4522b is a mobile power IC with integrated output for 2A charging and boosting
随机推荐
How to make knowledge map assist multi round dialogue
Kuangshi Research Institute | dark vision network: RGB IR Fusion low illumination imaging method using depth inconsistent a priori
插入创建数组
刷题整理(一)
R language generalized linear model GLM: linear least squares, logarithmic transformation, Poisson, binomial logistic regression analysis, ice cream sales time series data and simulation
2022年江西省安全员A证考试题及答案
Three special data types of redis -- bitmap calculation
On the happiness of fishing -- April 20
【Leetcode-每日一题】山羊拉丁文
TVS二极管的应用与分析
[untitled] 2022 simulated examination platform operation of work license of coal production and operation unit (electromechanical transportation safety management personnel)
Fansea 4W single coil transmitting wireless charging 5W module
院士专家热议如何拥抱“东数西算”,第二届中国IDC行业Discovery大会顺利召开
1.2V Ni MH battery lamp solar lawn lamp boost drive IC silk screen printing aatlb package SOT23-6 and sop8 single chip microcomputer IC
Academicians and experts discussed how to embrace "counting from the east to the west", and the second China IDC industry discovery conference was successfully held
08. 树莓派安装MySQL
分享我常用的28个自媒体工具,赶紧收藏,视频2万播放就有264收益
将node项目部署到云服务器详细教程 原创
Codesys méthode de lecture des fichiers csv (non Excel)
Operation of simulated examination platform of test question bank for electrical installation and maintenance of hoisting machinery in 2022