在继承表单上重写的控件在 VB.Net 中被调用两次

问题描述 投票:0回答:1

我正在编写一些继承表单并覆盖某些控件的代码。在下面的示例中,覆盖子函数被调用两次(消息框出现两次)。

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

由于未命中子节点上的断点,因此似乎未调用父子子节点。

有谁知道为什么会发生这种情况或如何解决? 有没有更好的方法来覆盖控件?

vb.net inheritance overriding
1个回答
0
投票

我会密切关注该线程以寻求解决方案,但现在我将使用 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
© www.soinside.com 2019 - 2024. All rights reserved.