博客
关于我
c#的事件(2021-3-18)
阅读量:609 次
发布时间:2019-03-13

本文共 541 字,大约阅读时间需要 1 分钟。

事件是一种消息机制

事件发布类(事件源)触发事件,别的类可以订阅(监听)事件
事件是一种特殊的多播委托实例
1.委托类型定义:
public delegate void ClickHanlder(object sender, ClickEventArgs args);(申明在类外)
在这里插入图片描述

2.事件的创建

在类中申明事件(创建委托实例)
public event ClickHandler OnClick;//OnClick为委托实例
在这个类中的某个方法里调用这个委托实例(事件)
在这里插入图片描述
3.事件的订阅
在申明事件所在类的外面订阅,只能用+和-两个运算符
在这里插入图片描述
注意:(1)这里的"button1.OnClick +=new ClickHandler(Btn_OnClick"和"button1.OnClick+=Btn_OnClick2"是订阅事件的两种不同写法,效果相同,后者是前者形式的简写
(2)可以往事件中添加多个处理方法,所以事件可以看成是多播委托。
(3)为事件添加处理方法的语句只能写在类的构造函数里,否则会报错:
在这里插入图片描述
4.main函数
在这里插入图片描述
定义了Form类对象form1,调用了form1.button1.Click()方法,相当于触发了form1.button1里的OnClick事件。

转载地址:http://fsuaz.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>