VB.NET 中同一事件的多个事件处理程序 [英] Multiple event handlers for the same event in VB.NET

查看:22
本文介绍了VB.NET 中同一事件的多个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 TextBox1

原因是第一个处理程序是多个 TextBox.Leave 事件的通用处理程序,用于验证值,第二个处理程序特定于上述 TextBox1 它会计算一些值.

The reason for this is that the first handler is a common one for multiple TextBox.Leave events which validates the values, and the second one is specific for the above TextBox1 which does some calculation of values.

我的问题是,当 TextBox1.Leave 发生时,我能知道两个处理程序中的哪一个将首先执行吗?

My query is that can I know which of the two handlers will execute first when TextBox1.Leave happens?

(我知道我可以将代码从通用处理程序中删除到 TextBox1 的特定处理程序中,但我仍然想知道是否有办法.)

(I know I can remove the code from the common handler to the specific one for TextBox1, but still I wish to know if there is a way.)

谢谢

推荐答案

只要使用 AddHandler 语句添加事件处理程序,就保证事件处理程序的调用顺序与他们被添加了.另一方面,如果您在事件处理程序方法上使用 Handles 修饰符,我认为没有任何方法可以确定顺序是什么.

As long as the event handlers are added using the AddHandler statement, the event handlers are guaranteed to be called in the same order that they were added. If, on the other hand, you are using the Handles modifier on the event handler methods, I don't think there is any way to be sure what the order will be.

下面是一个简单的例子,演示了由调用 AddHandler 的顺序决定的顺序:

Here's a simple example that demonstrates the order as determined by the order in which AddHandler is called:

Public Class FormVb1
    Public Class Test
        Public Event TestEvent()

        Public Sub RaiseTest()
            RaiseEvent TestEvent()
        End Sub
    End Class

    Private _myTest As New Test()

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddHandler _myTest.TestEvent, AddressOf Handler1
        AddHandler _myTest.TestEvent, AddressOf Handler2
        _myTest.RaiseTest()
        RemoveHandler _myTest.TestEvent, AddressOf Handler1
        RemoveHandler _myTest.TestEvent, AddressOf Handler2
    End Sub

    Private Sub Handler1()
        MessageBox.Show("Called first")
    End Sub

    Private Sub Handler2()
        MessageBox.Show("Called second")
    End Sub
End Class

这篇关于VB.NET 中同一事件的多个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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