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

查看:143
本文介绍了在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 语句,事件处理程序保证以与它们相同的顺序被调用。另一方面,如果您在事件处理程序方法中使用句柄修饰符,那么我不认为有什么办法可以确定顺序是什么。

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天全站免登陆