我有一段代码:
innerchannel.Closing += Function(sender, e)
RaiseEvent Closing(sender, e)
End Function
有问题
innerchannel.Closing
其中VisualStudio告诉我:
Public Event Closing(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
如何相应地修复它?
我假设您想为Closed事件添加一个处理程序:
AddHandler innerchannel.Closing, AddressOf Closed
- AddHandler语句
- 操作员地址
如果要引发自定义事件,例如在UserControl
:中
Class Channel
Inherits UserControl
Public Event Closing(ch As Channel)
' this could be for example a button-click handler
Protected Sub Closed(sender As Object, e As System.EventArgs)
RaiseEvent Closing(Me)
End Sub
End Class
在VB中不使用+=
来添加事件处理程序。您使用AddHandler
。
您的代码试图像调用函数一样调用Closing
,并对其结果执行加法运算。
firs必须将方法附加到事件处理程序,如下所示:
AddHandler innerchannel.Closing, AddressOf Closed
并使用RaiseEvent
来自msdn的注释:
如果事件未在其所在的模块内声明引发时,会发生错误。以下片段说明了一个事件声明和引发事件的过程。