如何写事件处理程序在VB.NET? [英] How to write event handler in VB.NET?
问题描述
我想测试一下很简单的事件处理在VB.NET。
I am trying to test out very simple event handling in VB.NET.
到目前为止,我有:
Public Delegate Sub TestEventDelegate()
Public Event TestEvent As TestEventDelegate
Sub MySub
Raise TestEvent
End Sub
你怎么会写的事件处理程序,只是显示一个简单的的MessageBox
上面的事件?
推荐答案
写的处理方法很简单 - 只写了小组
它不带任何参数,并显示一个消息框
Writing the handler method is simple - just write a Sub
which takes no parameters and displays a message box.
您再需要订阅的处理程序的情况下,你可以做任何添加的 手柄
子句的方法:
You then need to subscribe the handler to the event, which you can either do adding a Handles
clause to the method:
Sub ShowMessageBox() Handles foo.TestEvent
或使用 的AddHandler
语句:
AddHandler foo.TestEvent, AddressOf ShowMessageBox
请注意,要遵循.NET约定,您的代理应该有两个参数 - 类型的一个对象
来指定对象引发事件,并键入一个 EventArgs的
或子类,提供任何额外的信息。这不是的需要的由语言,但它是一个广泛的,其次约定。
Note that to follow .NET conventions, your delegate should have two parameters - one of type Object
to specify which object raised the event, and one of type EventArgs
or a subclass, to provide any extra information. This isn't required by the language, but it's a broadly-followed convention.
这篇关于如何写事件处理程序在VB.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!