Force Custom Event Handler First
Hi all!
When I create custom handlers like:
Code:
AddHandler Form1.MouseMove, AddressOf MoveMouse
I need MoveMouse to fire before any other event when the user moves their mouse over Form1.
Code:
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MsgBox("Needs to happen second.")
End Sub
Private Sub MoveMouse(sender As Object, e As MouseEventArgs)
MsgBox("Needs to happen first.")
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
Re: Force Custom Event Handler First
Rather than using AddHandler, just call your method in the MouseMove event:
Code:
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Me.MoveMouse(sender, e)
MsgBox("Needs to happen second.")
End Sub
Private Sub MoveMouse(sender As Object, e As MouseEventArgs)
MsgBox("Needs to happen first.")
End Sub
Re: Force Custom Event Handler First
There is no safe way to ensure the order of events that I am aware of. The control that raises the event will call the event handler in the order it finds it on the list. I've always assumed that this has something to do with the order of control creation in InitializeComponent(), but I don't know. If that is true, then any handler added with AddHandler would always end up later than those added via a Handles clause. Still, if there are two added with a Handles clause, I doubt you can be certain which will be called first, and you certainly shouldn't rely on any particular order, since you have so little control over it.
Re: Force Custom Event Handler First
Good workaround, but there's nothing sneaky I could add to this line to make sure it fires first? Just making sure. I don't know if I expected something or not.
Code:
AddHandler Form1.MouseMove, AddressOf MoveMouse
(Shaggy Hiker ninja'd me)
Re: Force Custom Event Handler First
I'll keep an eye on this thread, because if there's some way that I am not aware of, it would certainly be useful. I don't think it's possible, though.
Re: Force Custom Event Handler First
Another thought that popped into my head is, if you're wanting to add the handler to controls other than your Form, you could set a flag in the Tag property of the control and check for it in the Form1_MouseMove event like this.
Re: Force Custom Event Handler First
Quote:
...you could set a flag in the Tag property of the control and check for it in the Form1_MouseMove event...
How would one accomplish this? Sorry if that sounds very low-knowledge, but I have never used tags before.
Re: Force Custom Event Handler First
Remove the Handles clause from the generates event handler, then simply wire it up yourself. Use AddHandler to add yours first, then the original one second.
-tg