-
C#教程之C#窗体间常用的几种传值方式及委托与事件详解
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
前言
窗体间的传值,最好使用委托方式传值,开始之前,我们先来说一下委托与事件的关系。
委托:是一个类。
事件:是委托类型的一个特殊实例,只能在类的内部触发执行。
首先创建2个窗体,这里我们以form1为发送窗体,form2为接收窗体
form1窗体
form2窗体
方式一(最简单的方式)
form1窗体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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 Form2 : Form { public Form2() { InitializeComponent(); } public void SetTxt( string txt) { this .txtMsg.Text = txt; } } } |
方式二(委托方式)
注:委托不熟悉的宝宝们,请自行查阅Func与Action,以及delegate三者区别,这里我们用系统内置的委托Action
form1窗体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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 Action< string > afterMsgSend { get ; set ; } private void Form1_Load( object sender, EventArgs e) { Form2 f2 = new Form2(); afterMsgSend += f2.SetTxt; //给系统内置的委托注册事件 f2.Show(); } private void btnSendMsg_Click( object sender, EventArgs e) { if (afterMsgSend == null ) { return ; } afterMsgSend( this .txtMsg.Text); } } } |
form2窗体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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 Form2 : Form { public Form2() { InitializeComponent(); } public void SetTxt( string txt) { this .txtMsg.Text = txt; } } } |
方式三(事件方式,更安全哟)
TextBoxMsgChangeEventArg类继承EventArgs代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 事件的方式实现窗体间传值 { public class TextBoxMsgChangeEventArg:EventArgs { public string Text { get ; set ; } } } |
form1窗体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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 event EventHandler AfterMsgChange; private void Form1_Load( object sender, EventArgs e) { Form2 f2 = new Form2(); AfterMsgChange += f2.AfterTxtChange; f2.Show(); } private void btnSendMsg_Click( object sender, EventArgs e) { AfterMsgChange( this , new TextBoxMsgChangeEventArg() { Text = this .txtMsg.Text }); } } } |
form2窗体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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 Form2 : Form { public Form2() { InitializeComponent(); } public void AfterTxtChange( object sender,EventArgs e) { //拿到父窗体传来的文本,强转数据类型 TextBoxMsgChangeEventArg arg = e as TextBoxMsgChangeEventArg; this .SetTxt(arg.Text); } } } |
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日