在vb.net中访问声明的事件时遇到问题

时间:2014-08-11 16:07:16

标签: vb.net events delegates raiseevent

在vb.net中访问声明的事件时遇到问题。

请在下面举例说明。 (我修改了以下内容以使其变得明白,因为它是自定义控件开发的一部分)

Public Class Main
    Inherits ComboBox

    'Event handler for when an item check state changes.
    Public Event ItemCheck As ItemCheckEventHandler
    Private parentMainClass As Main
    Private cclb As controlClass

    Public Sub New(parentclass As Main)
        Me.parentMainClass = parentclass
        'Add a handler to notify our parent of ItemCheck events.
        AddHandler Me.cclb.ItemCheck, New System.Windows.Forms.ItemCheckEventHandler(AddressOf Me.cclb_ItemCheck)
    End Sub

    Private Sub cclb_ItemCheck(sender As Object, e As ItemCheckEventArgs)
        'If ccbParent.ItemCheck IsNot Nothing Then
            RaiseEvent parentMainClass.ItemCheck(sender,e)
        'End If
    End Sub

    Public Class controlClass
        Inherits CheckedListBox
    End Class
End Class

问题:RaiseEvent parentMainClass.ItemCheck(sender,e)此语句显示 - 即使存在,ItemCheck事件也不存在。

请指导。

谢谢

2 个答案:

答案 0 :(得分:1)

事件声明;

Public Event ItemCheck As ItemCheckEventHandler

应该是;

Public Event ItemCheck(sender As Object, e As ItemCheckEventArgs)

错误告诉您的是它无法将事件与事件处理程序签名匹配。

答案 1 :(得分:0)

从原始代码开始;

RaiseEvent parentMainClass.ItemCheck(sender, e)

应改为;

RaiseEvent ItemCheck(sender, e)

这会从当前实例引发ItemCheck事件。您似乎想要做的是在实例上引发事件,这有点不同。