抽象类的事件处理

发布于 2024-09-08 13:10:21 字数 2511 浏览 5 评论 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 到应该处理该事件的子窗体中。

我在这儿吗?

感谢您的阅读!

Basically, I have a custom child form class which has events that will be passed to the parent. In the custom child form, I have a declaration of a "MustInherit" class that inherits the DevExpress User Control Class.

The reason for this, is I have many user controls that derive from this base class, and the child form can have an instance of any one of these controls, and doesnt care which. The only requirement is that the child form can handle the same events from each type of control the same way.

Some watered down code snippets(still pretty long unfortunately):

'''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

In a nutshell, thats how I made the child form and how everything is suppose to work. But when I press the button in the inherited child control, the event only gets as far as the base class and never traverses the RaiseEvent into the child form thats suppose to handle the event.

Am I even in the ballpark here?

Thanks for reading!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2024-09-15 13:10:21

您忘记使用 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

You forgot to add your event handle by using AddHandler or Handles identifier. See below using the Handles cgChild.MoveToDocker identifier.


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文