当前位置:网站首页>C#窗体向另一个窗体实时传值
C#窗体向另一个窗体实时传值
2022-06-29 09:26:00 【zlbcdn】
1、功能展示
有时需要将子界面的内容传递到父界面,方法有好几种。经常用的是通过委托实现。具体的效果如下:
![]()
【说明】点击父界面上“打开子界面”button,则会弹出子界面。父界面与子界面如上图所示。
![]()
【说明】在子界面的textBox框内输入待返回至父界面的内容,点击“将内容返回”button,则父界面的textBox将会展示返回的内容。具体如上图所示
2、代码分析
里面应用了委托(delegate)和事件(event)。委托就是前端开发中最常用的“回调方法”(callback),event是一种注册机制,将动作与委托关联。
为了实现以上功能,首先先编写子窗体的定义。代码如下:
public partial class Form2 : Form { //第二步:声明一个委托类型的事件 public event setTextValue setFormTextValue; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //第三步:准备相关数据。 setFormTextValue(this.textBox1.Text); } } // 第一步:声明一个委托。(根据自己的需求) public delegate void setTextValue(string textValue);
第一步:先在子窗体中定义一个委托。根据实际需求定义委托
第二步:在子窗体中声明一个event,将委托与动作关联
第三步:在具体的事件中实现event
以上三步参见Form2的代码
父窗体的代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); //第四步:初始化事件 form2.setFormTextValue += new setTextValue(form2_setFormTextValue); form2.Show(); } //第五步:实现事件 void form2_setFormTextValue(string textValue) { //具体实现。 this.textBox1.Text = textValue; } }
在父窗体的具体实现中分为2步:
第四步:在定义子窗体时,声明子窗体的事件
第五步:实现具体的事件
以上,就是全部的内容
3、功能分析
通过委托的方式,优点:松耦合。
4、源码下载
【说明】因为CSDN会把旧代码下载的门槛自动提高至50金币,所以我把代码共享到百度云。具体链接如下
下载链接 提取码: zkeu
边栏推荐
- mysql中的if [not] exists
- 2021年团体程序设计天梯赛-模拟赛
- Codeforces Round #657 Div. 2
- 2019.10.27 training summary
- PGP在加密技术中的应用
- MySQL中update一条record的过程
- Wandering --- 最后的编程挑战
- September 29, 2020 non commodity templating code level rapidjson Library
- Codeforces Round #645 (Div. 2)
- HDU 4578 transformation (segment tree + skillful lazy tag placement)
猜你喜欢

Simulation problem of two stacks

nacos注册中心集群

Nacos registry cluster

这个开源项目超哇塞,手写照片在线生成

点在多边形内外的判断

Codeforces Round #645 (Div. 2)

2021 team programming ladder competition - Simulation Competition

Codeforces Round #659 (Div. 2)

EDA and VHDL question bank

Application of keil5 integrated development environment for single chip microcomputer
随机推荐
子串分值-超详细版——最后的编程挑战
Substring score - Ultra detailed version - the last programming challenge
Seaweedfs security configuration
Related problems of pointer array, array pointer and parameter passing
2019.10.16训练总结
Application of keil5 integrated development environment for single chip microcomputer
SymPy Tutorial(译)
1147 heaps (30 points)
Dynamic planning summary
JNI.h说明
Text of the basic component of the shutter
山科 的C语言2018练习题(电信)
mysql 8.0 一条insert语句的具体执行流程分析(二)
Analyze in detail the PBOT mining virus family behavior and the principle of exploited vulnerabilities, and provide detailed protection suggestions for the blue army
Wandering -- the last programming challenge
这个开源项目超哇塞,手写照片在线生成
C语言库函数--strstr()
两个栈的模拟题
2020-09-18 referer authentication URL escape
We can't tell the difference between distributed and cluster. Let's tell the story of two cooks cooking

