C# 委托例子
两个子窗口向一个主窗口发送信息
主窗口:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 fm2 = new Form2();
fm2.msgSender = this.Receiver; //[3]将委托对象和方法关联
fm2.Show(); Form3 fm3 = new Form3();
fm3.msgSender = this.Receiver; //[3]将委托对象和方法关联
fm3.Show();
} //[2]根据委托定义方法完成数据传递
public void Receiver(string info)
{
this.label2.Text = info;
} }
//[1]声明委托,一般定义在外面
public delegate void ShowInfo(string param);
}
子窗口1:
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//创建委托对象
public ShowInfo msgSender; //通过委托传递数据
private void button1_Click(object sender, EventArgs e)
{
msgSender(this.textBox1.Text);
} }
}
子窗口2:
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 WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} //创建委托对象[从到主]
public ShowInfo msgSender; private void button1_Click(object sender, EventArgs e)
{
msgSender(this.textBox1.Text);
} }
}
主窗口向两个子窗口发送信息

上代码~~~~~~~~~~
主窗口:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{ public Action<string> SendMsg { get; set; } public Form1()
{
InitializeComponent();
Form2 fm2 = new Form2();
SendMsg += fm2.Recevier;
fm2.Show(); Form3 fm3 = new Form3();
SendMsg += fm3.Receiver; //[3]将委托对象和方法关联
fm3.Show();
} private void button1_Click(object sender, EventArgs e)
{
if (SendMsg != null)
{
SendMsg(this.textBox1.Text);//执行所有注册的委托
}
} } }
子窗口1:
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} public void Recevier(string info)
{
this.label1.Text = info;
}
} }
子窗口2:
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 WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} public void Receiver(string info)
{
this.label1.Text = info;
} } }
C# 委托例子的更多相关文章
- c# 简单委托例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- c#.NET的事件与委托例子
原文发布时间为:2008-07-25 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- C# 委托详解(一)
1.委托简单例子 class eeProgram { // 声明delegate对象 public delegate string CompareDelegate(int a, int b); // ...
- 浅谈async、await关键字 => 深谈async、await关键字
前言 之前写过有关异步的文章,对这方面一直比较弱,感觉还是不太理解,于是会花点时间去好好学习这一块,我们由浅入深,文中若有叙述不稳妥之处,还请批评指正. 话题 (1)是不是将方法用async关键字标识 ...
- 理解C#事件
前面文章中介绍了委托相关的概念,委托实例保存这一个或一组操作,程序中将在某个特定的时刻通过委托实例使用这些操作. 如果做过GUI程序开发,可能对上面的描述会比较熟悉.在GUI程序中,单击一个butto ...
- 高级C#
使用delegates委托写插件方法: public delegate int Transformer (int x); public class Util { public static void ...
- IEnumerable和IQueryable的区别以及背后的ExpressionTree表达式树
关于IEnumerable和IQueryable的区别,这事还要从泛型委托Func<T>说起.来看一个简单的泛型委托例子: class Program { static void Main ...
- Javascript中常用方法简介
Array数组常用方法 先创建一个数组var abc = [1,2,3,4,5,6,7,8,9]; (1)pop(); 这个方法会删除数组的最后一项并返回删除掉的值. 比如:console ...
- Qt Delgate的使用 简单说明
(一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型基于QA ...
随机推荐
- 【Alpha版本】冲刺阶段——Day6
[Alpha版本]冲刺阶段--Day6 阅读目录 今日进展 问题困难 明日任务 今日贡献量 TODOlist [今日进展] 为注册模块增加界面代码 public static void Windows ...
- websocket 原理
自己写一个websocket import socket, hashlib, base64 sock = socket.socket() sock.bind(('127.0.0.1', 9000)) ...
- 102.自己实现ArrayList
package collection; import java.util.ArrayList; import java.util.List; /** * 自己实现一个ArrayList,帮助理解底层结 ...
- IPERF 网络性能测试
Iperf 是一个网络性能测试工具.Iperf可以测试最大TCP和UDP带宽性能.Iperf具有多种参数和UDP特性,可以根据需要调整.Iperf可以报告带宽,延迟抖动和数据包丢失. Iperf 参数 ...
- Tomcat配置Manager管理员
修改文件: D:\MyDev\Tomcat\apache-tomcat-7.0.68\conf\tomcat-users.xml 配置内容: <role rolename="mana ...
- Zynq ZC706 传统方式移植Linux -- 编译u-boot
我用的是zc706不是zed 基本思路是: 1.安装交叉编译工具(见 https://www.cnblogs.com/idyllcheung/p/10532654.html ) 2.下载xilinx ...
- 目标检测论文阅读:Deformable Convolutional Networks
https://blog.csdn.net/qq_21949357/article/details/80538255 这篇论文其实读起来还是比较难懂的,主要是细节部分很需要推敲,尤其是deformab ...
- usb帧格式
源: usb帧格式
- MySQL日志详细说明
这片博文我们会详细说明MySQL本身的日志,不包含重做日志和undo日志(这两个日志是innodb存储引擎的日志). MySQL本身的日志有以下几种(MySQL5.7版本): 错误日志 慢查询日志 通 ...
- Selenium在Firefox中踩过的
本文转至 http://www.51testing.com/html/11/n-3711311.html,作者对webdriver在Firefox中设置profile配置项挺熟的,是用Python实现 ...