委托传值
2016-06-28 16:38
169 查看
点击Form1的按钮,把文本框的值传给Form2。再点击Form2的按钮,把文本框的值传给Form1。
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.Windows.Forms; namespace 委托传值 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(txt1.Text,doSth);//Form2构造方法 frm2.Show(); } //把窗体2的字符串给窗体1 public void doSth(string msg) { txt1.Text = msg; } } }
Form2的代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 委托传值 { public delegate void MyDel(string str);//定义委托 public partial class Form2 : Form { public Form2() { InitializeComponent();//初始化窗体 } private MyDel _del;//实例化委托 public Form2(string str,MyDel del) { InitializeComponent();//初始化窗体 txt2.Text = str; this._del = del;//构造方法传参 } private void Form2_Load(object sender, EventArgs e) { } private void btn2_Click(object sender, EventArgs e) { //调用委托前先判定 if (this._del != null) { this._del(txt2.Text);//调用委托 } this.Close(); } } }
相关文章推荐
- [知其然不知其所以然-30] How to work around when system can not be woken up
- Oracle shutdown(关闭)状态下从spfile中创建pfile
- linux小知识之/etc/sudoers文件应用实例
- EMC virtual provisining and fast vp
- Android冷启动白屏解析,带你一步步分析和解决问题
- Eclipse 在开发中使用到的快捷键 提高开发效率
- 使用compass的遇到的坑
- Debian alternatives system: update-alternatives
- 解析PHP多种序列化与反序列化的方法
- spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类
- 15.7
- oracle 笔记(1)
- 舒服的RGB配色
- 转载-------GLIBC内存分配机制引发的“内存泄露”
- ios学习路线—ios沙盒(sandbox)机制和文件操作(一)
- 使用consul实现服务的注册和发现
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- 本地代码推送到github的命令
- Java Web集成第三方登录
- SDUTACM数据结构上机测试2-2:单链表操作B