Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Yes</strong>, the same method can handle multiple events, as long as they have compatible signatures. Since both the <code>MouseHover</code> and <code>MouseLeave</code> events have identical method signatures, this is easy.</p> <p>By method signatures, of course, I mean the arguments that are passed in. For example, here are the signatures for a method that handles both of those events:</p> <pre><code>Sub MouseHoverHandler(ByVal sender As Object, ByVal e As System.EventArgs) Sub MouseLeaveHandler(ByVal sender As Object, ByVal e As System.EventArgs) </code></pre> <p>Since those are identical, the same method can handle both events. All you have to do is add the names of both events after the <code>Handles</code> keyword, separating them with a comma. For example:</p> <pre><code>Private Sub MegaHandler(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles myControl.MouseHover, myControl.MouseLeave </code></pre> <p><br> But, <strong>alas</strong>, that <em>does</em> make it impossible to distinguish between the events, as the same handler will be called for both. This is often convenient when you want to execute identical code and don't care which individual event was raised.</p> <p>It is not a good option when you need to distinguish between the events. But there's absolutely nothing wrong with defining multiple event handler methods. It won't affect the performance of your application.</p> <hr> <p>Another option you could consider is attaching stub methods as the handlers for both of those events, and have those stubs call out to another method that does the actual work. Because each event would have its own individual handler, you would be able to determine which event was raised, and pass that information as a parameter to your worker method. Maybe an explanation would be clearer:</p> <pre><code>Private Sub MouseHoverHandler(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles myControl.MouseHover ' Call the method that does the actual work DoMouseWork(sender, e, True) End Sub Private Sub MouseLeaveHandler(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles myControl.MouseHover ' Call the method that does the actual work DoMouseWork(sender, e, False) End Sub Private Sub MegaMouseHandler(ByVal sender As System.Object, ByVal e As System.EventArgs, _ ByVal isHover As Boolean) ' Do the appropriate work to handle the events here. ' If the isHover parameter is True, the MouseHover event was raised. ' If the isHover parameter is False, the MouseLeave event was raised. End Sub </code></pre> <p>Bonus points for recognizing that specifying the type of event would be best implemented by passing an enum value to the mega-handler method, instead of a Boolean value. (Enums make your source code much more descriptive; you have to examine the signature of the <code>MegaMouseHandler</code> method to know what the Boolean parameter represents.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload