我正在编写一些继承表单并覆盖某些控件的代码。在下面的示例中,覆盖子函数被调用两次(消息框出现两次)。
' (ProjectBase.vbproj)
Public Class FormBaseMain
Protected Overridable Sub ButtonDoStuff_Click(sender As Object, e As EventArgs) Handles ButtonDoStuff.Click
' Do nothing for now
End Sub
End Class
' (TestApp.vbproj)
Public Class FormNewMain
Inherits ProjectBase.FormBaseMain
Private Sub FormNewMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RemoveHandler ButtonDoStuff.Click, AddressOf MyBase.ButtonDoStuff_Click ' Doesn't help
End Sub
Protected Overrides Sub ButtonDoStuff_Click(sender As Object, e As EventArgs) Handles ButtonDoStuff.Click
MsgBox("test")
End Sub
End Class
由于未命中子节点上的断点,因此似乎未调用父子子节点。
有谁知道为什么会发生这种情况或如何解决? 有没有更好的方法来覆盖控件?
我会密切关注该线程以寻求解决方案,但现在我将使用 hack 并继续前进。
' HACK: The sub handles each click event twice, first from this local project,
' then from the base project.
' The hack is the mouse click is captured by local project, but not base
' project, so if the sender does not report a mouse event capture, it must
' not be local call, and return from the sub.
If DirectCast(sender, System.Windows.Forms.Control).Capture = False Then Return