vb.net – Visual Studio 2015中的WithEvents错误?

我可能遇到了Visual Studio 2015的问题.如果使用WithEvents和Handles关键字定义同一事件的三个​​处理程序,则不会调用其中一个处理程序.我在我们的软件中跟踪了这种行为,并编写了以下WinForms示例项目.

Public Class MainBase1
    Inherits Form

    Protected WithEvents Button1 As Button
    Protected TextBox1 As TextBox

    Public Sub New()
        SuspendLayout()

        Text = "WithEvents Test"
        ClientSize = New Size(300, 300)

        Button1 = New Button()
        Button1.Location = New Point(10, 10)
        Button1.Size = New Size(100, 25)
        Button1.Text = "Button1"
        Controls.Add(Button1)

        TextBox1 = New TextBox()
        TextBox1.Location = New Point(10, 50)
        TextBox1.Multiline = True
        TextBox1.Size = New Size(280, 240)
        Controls.Add(TextBox1)

        ResumeLayout(False)
        PerformLayout()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text &= "MainBase1: Button click handled." & vbNewLine
    End Sub
End Class


Public Class MainBase2
    Inherits MainBase1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text &= "MainBase2: Button click handled." & vbNewLine
    End Sub
End Class


Public Class Main
    Inherits MainBase2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text &= "Main: Button click handled." & vbNewLine
    End Sub
End Class

单击Button1后,Textbox1包含

MainBase1: Button click handled.
Main: Button click handled.

如果我在Visual Studio 2012下编译相同的示例,我会得到

MainBase1: Button click handled.
MainBase2: Button click handled.
Main: Button click handled.

有没有人遇到过这个问题?还是我错过了什么?

最佳答案 这个bug仍然存在.这是我的解决方案.

我在MainBase1 Protected Overridable中创建了处理程序.子类不使用Handles但会覆盖处理程序并调用MyBase.

这种方法的优点是处理程序的执行顺序是明确定义的.

Public Class MainBase1
    Inherits Form

    Protected WithEvents Button1 As Button
    Protected TextBox1 As TextBox

    Public Sub New()
        SuspendLayout()

        Text = "WithEvents Test"
        ClientSize = New Size(300, 300)

        Button1 = New Button()
        Button1.Location = New Point(10, 10)
        Button1.Size = New Size(100, 25)
        Button1.Text = "Button1"
        Controls.Add(Button1)

        TextBox1 = New TextBox()
        TextBox1.Location = New Point(10, 50)
        TextBox1.Multiline = True
        TextBox1.Size = New Size(280, 240)
        Controls.Add(TextBox1)

        ResumeLayout(False)
        PerformLayout()
    End Sub

    Protected Overridable Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles Button1.Click
        TextBox1.Text &= "MainBase1: Button click handled." & vbNewLine
    End Sub
End Class


Public Class MainBase2
    Inherits MainBase1

    Protected Overrides Sub Button1_Click(sender As Object, e As EventArgs)
        MyBase.Button1_Click(sender, e)
        TextBox1.Text &= "MainBase2: Button click handled." & vbNewLine
    End Sub
End Class


Public Class Main
    Inherits MainBase2

    Protected Overrides Sub Button1_Click(sender As Object, e As EventArgs)
        MyBase.Button1_Click(sender, e)
        TextBox1.Text &= "Main: Button click handled." & vbNewLine
    End Sub
End Class
点赞