用委托实现窗体间传值

1.新建一个工程.在Form1中添加一个Label和一个Button.新建一个事件类,让它有一个string 类型的属性,用于传值.

1 ///ReturnValueEventArgs.cs
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Text;
 5 
 6 namespace test
 7 {
 8     public  class ReturnValueEventArgs:EventArgs 
 9     {
10         private string str;
11 
12         public string strMessage
13         {
14             get
15             {
16                 return str;
17             }
18             set
19             {
20                 str = value;
21             }
22         }
23     }
24 }
25 

2.新建一个窗体Form2.添加一个TextBox和一个Button.添加如下代码.

1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace test
10 {
11     public partial class Form2 : Form
12     {
13         public  event returnValueHandler returnValue;
14 
15         public Form2()
16         {
17             InitializeComponent();
18         }
19 
20         private void button1_Click(object sender, EventArgs e)
21         {
22             ReturnValueEventArgs ee = new ReturnValueEventArgs();
23             ee.strMessage = textBox1.Text;
24             returnValue(sender, ee);
25         }
26     }
27 }

3.在Form1中添加如下代码.

 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 
10 namespace test
11 {
12     public delegate void  returnValueHandler (object sender ,ReturnValueEventArgs e);
13     public partial class Form1 : Form
14     {
15         
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             Form2 frm = new Form2();
24             frm.Show();
25             frm.returnValue += new returnValueHandler(frm_returnValue);
26             
27 
28         }
29 
30         void frm_returnValue(object sender, ReturnValueEventArgs e)
31         {
32 
33             label1.Text = e.strMessage;
34             //throw new Exception("The method or operation is not implemented.");
35         }
36     }
37 }

转载于:https://www.cnblogs.com/kjun/archive/2010/08/22/1805846.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275922.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c++ explicit关键字_聊一聊 C++的特性 explicit 匿名空间

聊一聊 C的特性 explicit && 匿名空间explicit关键字首先看一下explicit的作用:explicit 是避免构造函数的参数自动转换为类对象的标识符,平时代码中并不是经常用到,但是,有时候就是因为这个,会造成一定的BUG出…

谷歌浏览器那些有趣的隐藏功能

大家好,我是若川(点这里加我微信 ruochuan12,长期交流学习)。今天推荐一篇实用文章。文末有抽奖。点击下方卡片关注我、加个星标,或者查看源码等系列文章。学习源码整体架构系列、年度总结、JS基础系列很多小伙伴说还是…

AppDelegate的模块化+瘦身

前言 关于iOS的模块化,要追溯到16年接触的BeeHive了,BeeHive将功能模块化,以module的形式进行构建,以performSelector:的形式进行module的事件响应,以protocol的形式进行module间的通信。可以说思路非常清晰…

yii mysql_Yii2框架操作数据库的方法分析【以mysql为例】

本文实例讲述了Yii2框架操作数据库的方法。分享给大家供大家参考,具体如下:准备数据库DROP TABLE IF EXISTS pre_user;CREATE TABLE pre_user(id int(11) AUTO_INCREMENT PRIMARY KEY,username varchar(255) NOT NULL,password varchar(32) NOT NULL DEF…

C++接口注意

1. 用Record接口,要注意 Packed的区别 2. cdecl和stdcall的区别 3. C导出的函数建议用C格式stdcall导出,使用Def文件定义名称 4. 用VS写的API dll要注意是否引用了MFC的DLL,否则会使LoadLibrary失败,并GetLastError后返回14001 Ap…

Vue 3.1.0 的 beta 版发布

大家好,我是若川(点这里加我微信 ruochuan12,长期交流学习)。昨晚尤大视频号直播说到vue 3.1.0 beta版发布了,今天分享这篇文章。也有小伙伴可能注意到了昨晚我一直在送礼物。点击下方卡片关注我、加个星标&#xff0c…

设计模式练习_设计练习是邪恶的

设计模式练习It was the final round of interviews. Or, so the candidate thought.这是采访的最后一轮。 或者,所以候选人认为。 She’d spent all day interviewing in our office. As the final interviewer, I walked her out the building. She seemed confi…

morningcat2018 LearningDocs

2019独角兽企业重金招聘Python工程师标准>>> LearningDocs 学习资料与文档 JCP(Java Community Process ,Java社区进程 ) https://www.jcp.org/en/home/index JSR(Java Specification Requests,Java规范请求…

firefox下可恶的value

前几天做项目 遇到这样一个情况 document.getElementById("txtTest").value "111"; 这条语句在ff下和ie下都是好用的 但是用开发工具看html代码 ie下显示正常 ff下显示的却是修改之前的(实际上已经修改了,只是html没有修改过来) 用js修改input…

据说 99% 的人不知道 vue-devtools 还能直接打开对应组件文件?本文原理揭秘

1. 前言你好,我是若川[1],微信搜索「若川视野」关注我,专注前端技术分享,一个愿景是帮助5年内前端开阔视野走向前列的公众号。欢迎加我微信ruochuan12,长期交流学习。这是学习源码整体架构系列 之 launch-editor 源码&…

mysql 存储 事务_MYSQL 可以在存储过程里实现事务控制吗

展开全部6.7 MySQL 事务与锁定命令6.7.1 BEGIN/COMMIT/ROLLBACK 句法缺省的,MySQL 运行在 autocommit 模式。这就意味着,当你执行完一e69da5e887aa62616964757a686964616f31333361326265个更新时,MySQL 将立刻将更新存储到磁盘上。如果你使用…

如何忽略证书继续访问_前5个最容易被忽视的可访问性问题

如何忽略证书继续访问Accessibility is quickly becoming one of the most important aspects of the way we use the web, if not the most important. Just between 2017 and 2018, the number of federal court cases regarding web accessibility nearly tripled, signifyi…

《认清C++语言》のrandom_shuffle()和transform()算法

1&#xff09;STL中的函数random_shuffle()用来对一个元素序列进行重新排序&#xff08;随机的&#xff09;&#xff0c;函数原型如下&#xff1a; template<class RandomAccessIterator> void random_shuffle( RandomAccessIterator _First, //指向序列首元素的迭代器 R…

作为前端开发,如何高效学习 TypeScript

大家好&#xff0c;我是若川。有朋友跟我说最近面试前端候选人&#xff0c;问到关于 JavaScript 的一些少见误区问题&#xff0c;候选人很多都没回答上来&#xff0c;他很诧异&#xff0c;一个从国际大厂出来的面试者&#xff0c;竟然对 JavaScript 的一些误区问题都不了解。他…

figma下载_对于这10家公司,Figma是迈向新高度的起点

figma下载Hey everyone! In this post, we are highlighting 10 companies for which the use of Figma has become the starting point on the path to new heights. These are the use cases of problems and their solutions, where Figma played a decisive role.嘿大家&am…

mysql查询条件为or_使用mysql查询where条件里的or和and

为什么要着重讲这块内容呢?因为好多小伙伴都会混淆&#xff0c;要不就是不知道怎么组合使用&#xff0c;今天就给大家讲这部分内容干货&#xff0c;让大家半分钟看懂。AND、OR运算符的组合使用在WHERE子句中&#xff0c;通过AND、OR运算符可以同时连接多个条件&#xff0c;当然…

sql server(常用)

普通用法 //生成 uuid 并转为小写 select LOWER(SUBSTRING(uuid,1,8)-SUBSTRING(uuid,10,4)-SUBSTRING(uuid,15,4)-SUBSTRING(uuid,20,4)-SUBSTRING(uuid,25,12)) from (select cast(NEWID() as varchar(36)) as uuid) s //ea52a7bb-a2aa-44b8-be28-5ebc64defcf9//获取时分秒…

代码编写中会遇到的安全性问题

一、常用的攻击手段 1&#xff0e;脚本注入 漏洞描述&#xff1a; 脚本注入攻击在通过浏览器使用用户输入框插入恶意标记或脚本代码时发生。 如&#xff1a;某个输入框允许用户向数据存储中插入内容&#xff0c;如果将一段js脚本插入其中&#xff0c;则当其他用户使用或浏览此数…

TypeScript 原来可以这么香?!

先问一个问题&#xff0c;JavaScript有几种数据类型&#xff1f;number、string、boolean、null、undefined、symbol、bigint、object其中 bigint 是 ES2020 新增的数据类型&#xff0c;而早在 TS3.2 时便成为 TS 的标准&#xff0c;其实还有好多 ES 标准是 TS 率先提出的&…

java8新特性stream深入解析

2019独角兽企业重金招聘Python工程师标准>>> 继续java8源码的发烧热&#xff0c;越看越是有充实的感觉。 数据时代下的产物 Java顺应时代的发展推出的高效处理大量数据能力的api&#xff0c;它专注于对集合对象进行各种非常便利、高效的聚合操作&#xff0c;借助于同…