0

基本上,我有一个自定义子表单类,其中包含将传递给父级的事件。在自定义子窗体中,我声明了一个继承 DevExpress 用户控件类的“MustInherit”类。

这样做的原因是,我有许多从这个基类派生的用户控件,并且子窗体可以拥有这些控件中的任何一个的实例,而不关心哪个。唯一的要求是子窗体可以以相同的方式处理来自每种类型控件的相同事件。

一些淡化的代码片段(不幸的是仍然很长):

'''Inherited Class
Public Class ChildControlInheritedClass
    'A Button Click event that starts the chain of events.
    Private Sub btnMoveDocker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertToTab.Click
        OnMoveToDocker(Me, New ChildGridMoveArgs(Me))
    End Sub
End Class

'''Base Class
Public MustInherit Class ChildControlBaseClass
    Inherits DevExpress.XtraEditors.XtraUserControl
    Public Class ChildGridMoveArgs
        Inherits System.EventArgs
        Public Sub New(ByVal _ChildControl As ChildControlInheritedClass)
            ChildControl = _ChildControl
        End Sub
        Public ChildControl As ChildControlInheritedClass
    End Class
    Public Event MoveToDocker(ByVal sender As Object, ByVal e As ChildGridMoveArgs)
    Protected Overridable Sub OnMoveToDocker(ByVal sender As Object, ByVal e As ChildGridMoveArgs)
        '''Once this RaiseEvent is fired, nothing happens. The child form is oblivious.
        RaiseEvent MoveToDocker(sender, e)
    End Sub
End Class

'''Child Form Class
Public Class ChildForm
    Private WithEvents cgChild As ChildControlBaseClass
    Public Property ChildGrid() As ChildControlInheritedClass
        Get
            Return cgChild
        End Get
        Set(ByVal value As ChildControlInheritedClass)
            RemoveHandler cgChild.MoveToDocker, AddressOf cgChild_MoveToDocker
            cgChild.Dispose()
            cgChild = Nothing
            cgChild = value
            AddHandler cgChild.MoveToDocker, AddressOf cgChild_MoveToDocker
        End Set
    End Property
    Public Event MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs)
    Public Sub cgChild_MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs)
        RaiseEvent MoveToDocker(sender, New ChildControlInheritedClass.ChildGridMoveArgs(cgChild))
    End Sub
End Class

Public Class frmMain
    Private Sub OpenNewWindow()
        Dim frm As New ChildForm
        Dim chld As New ChildControlInheritedClass
        frm.ChildGrid = chld
        frm.Show()
    End Sub
End Class

简而言之,这就是我制作子表单的方式以及一切应该如何工作的方式。但是当我按下继承的子控件中的按钮时,事件只会到达基类,并且永远不会将 RaiseEvent 遍历到假设处理事件的子窗体中。

我什至在球场上吗?

谢谢阅读!

4

1 回答 1

0

您忘记使用 AddHandler 或 Handles 标识符添加事件句柄。使用 Handles cgChild.MoveToDocker 标识符见下文。


Public Class ChildForm
    ...
    Public Sub cgChild_MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs) Handles cgChild.MoveToDocker
        RaiseEvent MoveToDocker(sender, New ChildControlInheritedClass.ChildGridMoveArgs(cgChild))
    End Sub
End Class

于 2010-07-07T11:26:19.667 回答