gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:35:21 27 4
gpt4 key购买 nike

我可能遇到了 Visual Studio 2015 的问题。如果使用 WithEventsHandles 关键字定义同一事件的三个​​处理程序,则不会调用其中一个处理程序。我在我们的软件中跟踪了这种行为,并编写了以下 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

点击Button1Textbox1包含

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

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

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

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

最佳答案

这个错误仍然存​​在。这是我的解决方案。

我在 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

关于vb.net - Visual Studio 2015 中的 WithEvents 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com