-
VB.net学习笔记(七)对象事件的定制
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
这是Button控件的Click事件。注意上面两个地方:
Private Sub AAAAAXXXXX(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
上面用AAAAXXXXX,事件一样生效,所以方法名是不重要的。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click
上面的写法,唯一的要求是,每个事件的参数个数及类型必须是相同的。
为了能让线程执行调用不到的事件,那就建立委托吧
委托就产生了
委托就产生了
先定义一个委托体:Delegate Sub SetTxtOutputCallback(ByVal ksks As String)
委托了,就不会冲突了
Public Delegate Sub WalkedEventHandler(ByVal distance As Int32)
Private mWalkedHandlers As WalkedEventHandler
Public Custom Event Walked As WalkedEventHandler
AddHandler(ByVal value As WalkedEventHandler)
mWalkedhandlers = CType([delegate].combine(mWalkedhandlers, value), _WalkedEventHandler)
End AddHandler
RemoveHandler(ByVal value As WalkedEventHandler)
mwalkedhandlers = CType([delegate].remove(mWalkedHandlers, value), WalkedEventHandler)
End RemoveHandler
RaiseEvent(ByVal distance As Integer)
If mWalkedHandlers isnot Nothing thdn
mWalkedHandlers.invoke(distance)
End If
End RaiseEvent
End Event
当我们引发事件RaiseEvent时,可能误认为是由多线程来完成的,其实不然,这个过程只是一个线程,
引发事件类似调用方法,正如调用函数一样,返回后再继续向下执行。
下面执行的顺序是:1234567
Public Class Form1
Private mobjPerson As Person
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mobjPerson = New Person()
AddHandler mobjPerson.Walked, AddressOf OnWalk '动态加入事件(作用与withEvents与Handles链接类似)
End Sub
Private Sub OnWalk(ByVal Distance As Integer)
MessageBox.Show("Person walked " & Distance)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mobjPerson.Walk(42)
End Sub
End Class
Public Class Person
Private mintTotalDistance As Integer
Public Event Walked(ByVal Distance As Integer)
Public Sub Walk(ByVal Distance As Integer)
mintTotalDistance += Distance
RaiseEvent Walked(Distance)
End Sub
End Class
If Micro
soft.VisualBasic.Command = "nodisplay" Then
RemoveHandler mobjPerson.Walked, AddressOf LongOnWalk
Else
RemoveHandler mobjPerson.Walked, AddressOf OnWalk
End If
mobjPerson = New Person
Public Sub New()
Phone("home") = "555-1234"
Phone("work") = "555-5678"
End Sub
Public Sub New(ByVal Name As String, ByVal BirthDate As Date)
mstrName = Name
mdtBirthDate = BirthDate
Phone("home") = "555-1234"
Phone("work") = "555-5678"
End Sub
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比