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 ...
随机推荐
- HashMap 和 ConcurrentHashMap比较
基础知识: 1. ConcurrentHashMap: (JDK1.7) segment数组,分段锁:segment 内部是 HashEnty数组,类似HashMap: 统计长度的方法,先不加锁统计两 ...
- 如何干净卸载mysql
一.在控制面板中卸载mysql软件: 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了: 三.windows+R运行“reged ...
- 使用github管理Eclipse分布式项目开发
使用github管理Eclipse分布式项目开发 老关我在前面的博文(github管理iOS分布式项目开发)中介绍了github管理iOS分布式开发,今天老关将向大家介绍使用github管 理Ecli ...
- jvm 工作原理
作为一名Java使用者,掌握JVM的体系结构也是必须的. 说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成:Java编程语言.Java类文件格式.Java ...
- 怎样从外网访问内网MySQL数据库?
本地安装了一个MySQL数据库,只能在局域网内访问到,怎样从外网也能访问到本地的MySQL数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动MySQL数据库 默认安装的MySQL ...
- python selenium chrome有界面与无界面模式
from selenium.webdriver.chrome.options import Options from selenium import webdriver # 无界面模式 def Chr ...
- NGINX转发代理情况下,获取客户单真实IP
编译时加上http_realip_module 模块 realip模块生效的前提是:直接连接nginx的ip是在set_real_ip_from中指定的. 原机配置: set_real_ip_from ...
- Prometheus监控学习笔记之Prometheus监控简介
0x00 Prometheus容器监控解决方案 Prometheus(普罗米修斯)是一个开源系统监控和警报工具,最初是在SoundCloud建立的.它是一个独立的开放源码项目,并且独立于任何公司.不同 ...
- wait与sleep的区别
1.这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线程里调用了b的sleep方法,实际上还 ...
- 【题解】Luogu P2572 [SCOI2010]序列操作
原题传送门:P2572 [SCOI2010]序列操作 这题好弱智啊 裸的珂朵莉树 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 操作1:把区间内所有数推平成0,珂朵莉树基本操作 ...