资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net事件委托的简单介绍

VB.NET 中事件委托问题

address of 顾名思义,就是地址指向,每个函数都有一个地址,address of后面加函数名称。

我们注重客户提出的每个要求,我们充分考虑每一个细节,我们积极的做好网站制作、成都网站建设服务,我们努力开拓更好的视野,通过不懈的努力,创新互联赢得了业内的良好声誉,这一切,也不断的激励着我们更好的服务客户。 主要业务:网站建设,网站制作,网站设计,小程序开发,网站开发,技术开发实力,DIV+CSS,PHP及ASP,ASP.Net,SQL数据库的技术开发工程师。

handels 事件,你看看按钮的单击事件,窗体的启动事件,每个后面都有这个。

delegate 就是声明一个委托了。

我也不好详细说,其实你上Baidu搜这几个关键字加上点注解,比如“delegate的用法”,N多!

vb.net 如何取消事件的委托?

可以在选定全部子节点前,发送一个变量给全部子节点(有个tag属性可以利用),告诉它们不应该执行某事件(if语句)。

委托是可用于调用其他对象方法的对象。它们有时被称为类型安全函数指针,因为它们与其他编程语言中所使用的函数指针相似。但不同于函数指针,Visual Basic .NET 委托是基于 System.Delegate 类的引用类型,它可以引用共享方法 —无需特定的类实例即可调用的方法和实例方法。

委托在调用过程和被调用过程需要媒介的情况下是很有用的。例如,您可能想让一个引发事件的对象能够在不同的环境下调用不同的事件处理程序。不幸的是,引发事件的对象无法提前知道处理特定事件的事件处理程序。Visual Basic .NET 通过在使用 AddHandler 语句时创建委托,可让您动态地将事件处理程序与事件关联。在运行时,委托将各种调用转发到相应的事件处理程序。

尽管可以创建自己的委托,但在大多数情况下,Visual Basic .NET 为您创建委托并提供具体信息。例如,Event 语句将名为 EventNameEventHandler 的委托类隐式定义为 Event 语句所在类的嵌套类,且其签字与该事件相同。AddressOf 语句则隐式创建委托的实例。例如,以下两行代码是等效的:

AddHandler Button1.Click, AddressOf Me.Button1_Click

' AddHandler 指向引发事件的对象,AddressOf则确定该事件对象所要调用的事件处理程序

'上述行为又可以称为 监 视

AddHandler Button1.Click, New EventHandler(AddressOf Button1_Click)。

vb.net中如何取消事件委托

不能透过e来屏蔽不需要的事件?

死循环是怎么防止的?道理一样吧

可以在选定全部子节点前,发送一个变量给全部子节点(有个tag属性可以利用),告诉它们不应该执行某事件(if语句)。

没测试,不知道行不行。

vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。

一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法。

创建委托和匹配过程

创建一个名为 MySubDelegate 的委托。

Delegate Sub MySubDelegate(ByVal x As Integer)

声明一个类,该类包含与该委托具有相同签名的方法。

Class class1

Sub Sub1(ByVal x As Integer)

MsgBox("The value of x is: " CStr(x))

End Sub

End Class

定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。

Protected Sub DelegateTest()

Dim c1 As New class1

' Create an instance of the delegate.

Dim msd As MySubDelegate = AddressOf c1.Sub1

' Call the method.

msd.Invoke(10)

End Sub

二、事件

下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。

该示例程序使用事件和委托和引发事件中详细说明的概念。

示例

' EventSample.vb.

'

Option Explicit

Option Strict

Imports System

Imports System.ComponentModel

Imports Microsoft.VisualBasic

Namespace EventSample

' Class that contains the data for

' the alarm event. Derives from System.EventArgs.

'

Public Class AlarmEventArgs

Inherits EventArgs

Private _snoozePressed As Boolean

Private nrings As Integer

'Constructor.

'

Public Sub New(snoozePressed As Boolean, nrings As Integer)

Me._snoozePressed = snoozePressed

Me.nrings = nrings

End Sub

' The NumRings property returns the number of rings

' that the alarm clock has sounded when the alarm event

' is generated.

'

Public ReadOnly Property NumRings() As Integer

Get

Return nrings

End Get

End Property

' The SnoozePressed property indicates whether the snooze

' button is pressed on the alarm when the alarm event is generated.

'

Public ReadOnly Property SnoozePressed() As Boolean

Get

Return _snoozePressed

End Get

End Property

' The AlarmText property that contains the wake-up message.

'

Public ReadOnly Property AlarmText() As String

Get

If _snoozePressed Then

Return "Wake Up!!! Snooze time is over."

Else

Return "Wake Up!"

End If

End Get

End Property

End Class

' Delegate declaration.

'

Public Delegate Sub AlarmEventHandler(sender As Object, _

e As AlarmEventArgs)

' The Alarm class that raises the alarm event.

'

Public Class AlarmClock

Private _snoozePressed As Boolean = False

Private nrings As Integer = 0

Private stopFlag As Boolean = False

' The Stop property indicates whether the

' alarm should be turned off.

'

Public Property [Stop]() As Boolean

Get

Return stopFlag

End Get

Set

stopFlag = value

End Set

End Property

' The SnoozePressed property indicates whether the snooze

' button is pressed on the alarm when the alarm event is generated.

'

Public Property SnoozePressed() As Boolean

Get

Return _snoozePressed

End Get

Set

_snoozePressed = value

End Set

End Property

' The event member that is of type AlarmEventHandler.

'

Public Event Alarm As AlarmEventHandler

' The protected OnAlarm method raises the event by invoking

' the delegates. The sender is always this, the current instance

' of the class.

'

Protected Overridable Sub OnAlarm(e As AlarmEventArgs)

RaiseEvent Alarm(Me, e)

End Sub

' This alarm clock does not have

' a user interface.

' To simulate the alarm mechanism it has a loop

' that raises the alarm event at every iteration

' with a time delay of 300 milliseconds,

' if snooze is not pressed. If snooze is pressed,

' the time delay is 1000 milliseconds.

'

Public Sub Start()

Do

nrings += 1

If stopFlag Then

Exit Do

Else

If _snoozePressed Then

System.Threading.Thread.Sleep(1000)

If (True) Then

Dim e As New AlarmEventArgs(_snoozePressed, nrings)

OnAlarm(e)

End If

Else

System.Threading.Thread.Sleep(300)

Dim e As New AlarmEventArgs(_snoozePressed, nrings)

OnAlarm(e)

End If

End If

Loop

End Sub

End Class

' The WakeMeUp class has a method AlarmRang that handles the

' alarm event.

'

Public Class WakeMeUp

Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)

Console.WriteLine((e.AlarmText + ControlChars.Cr))

If Not e.SnoozePressed Then

If e.NumRings Mod 10 = 0 Then

Console.WriteLine(" Let alarm ring? Enter Y")

Console.WriteLine(" Press Snooze? Enter N")

Console.WriteLine(" Stop Alarm? Enter Q")

Dim input As String = Console.ReadLine()

If input.Equals("Y") Or input.Equals("y") Then

Return

Else

If input.Equals("N") Or input.Equals("n") Then

CType(sender, AlarmClock).SnoozePressed = True

Return

Else

CType(sender, AlarmClock).Stop = True

Return

End If

End If

End If

Else

Console.WriteLine(" Let alarm ring? Enter Y")

Console.WriteLine(" Stop Alarm? Enter Q")

Dim input As String = Console.ReadLine()

If input.Equals("Y") Or input.Equals("y") Then

Return

Else

CType(sender, AlarmClock).Stop = True

Return

End If

End If

End Sub

End Class

' The driver class that hooks up the event handling method of

' WakeMeUp to the alarm event of an Alarm object using a delegate.

' In a forms-based application, the driver class is the

' form.

'

Public Class AlarmDriver

Public Shared Sub Main()

' Instantiates the event receiver.

Dim w As New WakeMeUp()

' Instantiates the event source.

Dim clock As New AlarmClock()

' Wires the AlarmRang method to the Alarm event.

AddHandler clock.Alarm, AddressOf w.AlarmRang

clock.Start()

End Sub

End Class

End Namespace

vb.net 子类事件委托访问父类私有程序问题

1、是的,作用域的区别如下:

Public 公有成员,表示所有模块的所有其它过程都可访问这个成员。

Private

私有成员,表示只在其类的块中,唯有友元(Friend)才可以访问,属私有对象。其他地方均不可以访问。

Protected 半私有成员,表示只在其类的块中,或者是派生类中,友元才能访问。

Friend

友元,设置成友元的情况下下,可以不受public跟private的约束,可以进行私有成员的访问。

所以两个不同的模块中唯有 Public 是可以互相访问的。

2、能触发。

Private 是相对于其他类来讲是 Private 的,对于 C1 本身是可以访问的。既然 class2 是 C2 的实例,并继承了 C1,那么是可以触发 C1 中的 Private 过程的。


网站栏目:vb.net事件委托的简单介绍
文章URL:http://cdkjz.cn/article/dooggpe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220