Visual Studio 2015中的WithEvents错误? [英] WithEvents bug in Visual Studio 2015?

查看:62
本文介绍了Visual Studio 2015中的WithEvents错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I probably encountered an issue of Visual Studio 2015. If you define three handlers of the same event using WithEvents and Handles keywords, one of the handlers will not be called. I traced this behavior in our software and wrote following WinForms sample project.

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下编译相同的示例,则会得到

If I compile the same sample under Visual Studio 2012, I get

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

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

Has anybody encountered this issue as well? Or have I missed something?

推荐答案

此错误仍然存​​在.这是我的解决方法.

This bug still remains. Here is my solution to this.

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

I made the handler in MainBase1 Protected Overridable. Subclasses don't use Handles but override the handler and call MyBase.

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

This approach has the advantage that execution order of handlers is well-defined.

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

这篇关于Visual Studio 2015中的WithEvents错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆