在VB.NET中同一事件的多个事件处理程序

编程入门 行业动态 更新时间:2024-10-23 20:29:50
本文介绍了在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中同一事件的多个事件处理程序

本文发布于:2023-10-27 00:08:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531745.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   多个   程序   VB   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!