抽象类的事件处理
基本上,我有一个自定义子表单类,其中的事件将传递给父级。在自定义子窗体中,我有一个继承 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记使用 AddHandler 或 Handles 标识符添加事件句柄。请参阅下文,使用 Handles cgChild.MoveToDocker 标识符。
You forgot to add your event handle by using AddHandler or Handles identifier. See below using the Handles cgChild.MoveToDocker identifier.