首先强制自定义事件处理程序



当我创建自定义处理程序时,例如:

Public Class MyCustomClass
Public Sub AddHandlers()
AddHandler Form1.MouseMove, AddressOf MoveMouse
End Sub
Private Sub MoveMouse(sender As Object, e As MouseEventArgs)
MsgBox("Needs to happen first.")
End Sub
End Class

我需要此类中的 MoveMouse 在用户将鼠标移到 Form1 上时在任何其他事件之前触发。

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MsgBox("Needs to happen second.")
End Sub

在编写本文时,我意识到我可以在 Form1 的类中创建另一个自定义事件处理程序,但是有没有其他方法可以确保 MoveMouse(无论它在哪个类中(在Form1_MouseMove之前发生?

谢谢-~网卡

事件按声明顺序触发:

因此,如果您希望自定义类在 Form1 引发事件之前在 Form1 上引发MouseMove,则需要先使自定义类添加处理程序:

Public Class CustomClass
Public Sub OnMouseMoved(sender As Object, e As MouseEventArgs)
Console.WriteLine("Custom mouse moved")
End Sub
End Class
Public Class Form1
Public Custom As CustomClass
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Custom = New CustomClass
AddHandler MouseMove, AddressOf Custom.OnMouseMoved
AddHandler MouseMove, AddressOf OnMouseMoved
End Sub
Private Sub OnMouseMoved(sender As Object, e As MouseEventArgs)
Console.WriteLine("Form1 mouse moved")
End Sub
End Class

相关内容

  • 没有找到相关文章

最新更新