Results 1 to 8 of 8

Thread: Force Custom Event Handler First

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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)

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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.
    My usual boring signature: Nothing

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Force Custom Event Handler First

    ...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.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width