
正文
vbnet引用事件的简单介绍
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。
一委托vbnet引用事件:此示例演示如何将方法与委托关联然后通过委托调用该方法。
创建委托和匹配过程
创建一个名为 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
声明一个类vbnet引用事件,该类包含与该委托具有相同签名的方法。
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
相关问答
Q1: 请教下vb.net 什么时候用事件,raiseevent和直接调用函数有多大区别,谢谢
raiseevent
在合适的地方或时机触发事件,以执行已与该事件绑定的所有函数。
事件一旦被触发,将执行所有与该事件有关的函数,同一个事件执行的函数代码可以完全不同,开发者可以通过一个事件去做不同的事情。
——
开发一个按钮类,并定义好一个点击事件,通过用户点击按钮类来触发事件,最后创建两个按钮的实例:按钮1和按钮2。
点击按钮1,点击事件执行弹出一个消息框的代码。
点击按钮2,点击事件执行打开一个文件的代码。
若点击按钮不触发事件,而是执行某一定义好的函数paly,在点击后,按钮1和按钮2将只能执行相同的函数play。
浅见,通过一个事件可以做不同的事情,通过一个函数只可以做该函数规定的事情,除非每次都去修改函数,如果拥有该事件的类\控件是别人开发的,在别人没有提供源代码的情况之下,那根本不可能修改。
Q2: VB.NET引用
Private Function ReadFile(ByRef a As String) As String
a = "bian"
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = "yuanlai"
ReadFile(a)
''想要的结果就是在这里使用a的值是"bian"
MsgBox(a)
End Sub
===================
以上就可以,不知道你“ByRef a As b”的b是什么东西
Q3: 如何在VB.NET中调用别的控件的事件
这些问题的确头大,vb的dll、ocx等,在vb.net里都不好调用,主要是来要注册组件等,麻烦。建议全部用vb.net写就好了,
Q4: VB.net中怎么调用控件的事件
软糖
来回答罗。
“调用事件”有歧义vbnet引用事件,你的意思是不是下面两种:
调用事件处理方法
直接调用对象名.方法名
'例如有这样一个按钮点击事件处理程序
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
秒表.Start()
End Sub
'调用它直接用
Button1_Click(me, new EventArgs())引发事件
Button1.Click.Invoke(me, new EventArgs())另外 RaiseEvent
也可以引发事件vbnet引用事件,但不能使用
RaiseEvent 来引发派生窗体的控件事件,只有自己定义的类的事件才能用它引发。
如满意,请采纳,谢谢。
Q5: vb.net:声明事件和引用事件分别用的什么语句
声明事件只需用
private event 事件名称(参数表)
在通用部分声明
引用这个词不太恰当,应该说是触发
在需要触发的地方用
raiseevent 事件名称(参数表)就可以了
关于vbnet引用事件和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






