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

Force Custom Event Handler First

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

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

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

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

在写这篇文章时,我意识到我可以在 Form1 的 class 中创建另一个自定义事件处理程序,但是有没有其他方法可以确保 MoveMouse(不管它在什么 class 中)发生在 Form1_MouseMove?

之前

谢谢- ~尼克

事件按照声​​明的顺序触发:

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

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