0

When I create custom handlers like:

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

I need MoveMouse in this class to fire before any other event when the user moves their mouse over Form1.

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

While writing this, I realized I could create yet another custom event handler in Form1's class, but is there any other way to ensure that MoveMouse (regardless of what class it is in) happens before Form1_MouseMove?

Thanks- ~Nic

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • You are adding a handler to the same event - 2 handlers for the same event which is pointless - just add code to the existing event. Note that `AddHandler` adds a handler (!) so the one already there will fire first. – Ňɏssa Pøngjǣrdenlarp Jul 14 '17 at 21:05
  • I know. This is assuming there is a reason to have 2 handlers. I'm planning to make an application extension file which will handle certain events from the form but require them to fire first. –  Jul 14 '17 at 21:09
  • I have no idea what an 'application extension' is but events are private to the form other parts of the same app are not even aware of them: `Private Sub MoveMouse(...)` – Ňɏssa Pøngjǣrdenlarp Jul 14 '17 at 21:12
  • I want to create a DLL file which will customize the way the user interacts with the form. It's merely a practice project using a few things I've learned and to hopefully learn more things on the way. –  Jul 14 '17 at 21:20
  • `Sub MoveMouse` would not exist in `Class Form1`, it would be written in the other project I am working on. This is example code but I thought my question was fairly clear. –  Jul 14 '17 at 21:21

1 Answers1

0

Events are fired in the order in which they are declared:

So if you want your custom class to raise MouseMove on Form1 before Form1 raises the event you need to make your custom class add the handler first:

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
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • This is more or less what I'm looking for, but more just reassurance that there's no other factors at play than simply the order of `AddHandler` you use. –  Jul 15 '17 at 04:20
  • @NinjaNic : Not really, no. It more or less [**combines**](https://msdn.microsoft.com/en-us/library/30cyx32c(v=vs.110).aspx) the delegates (event handlers) as you add them, thus making the first one you added getting called first. – Visual Vincent Jul 15 '17 at 07:33