Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:44:49+00:00 2026-05-17T06:44:49+00:00

When I utilize AddHandler in VB to add my own method to the Click

  • 0

When I utilize AddHandler in VB to add my own method to the Click event :

  AddHandler Button.Click, AddressOf myButton_Click

I see that my code executes last – after other event handlers for the Button_Click event.
Is there a way to insert my event handler in front of other events so that it executes first?

I tagged this question as C# as well as VB, please feel free to use eitherlanguage if you have any suggestions.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:44:50+00:00Added an answer on May 17, 2026 at 6:44 am

    The order of execution of handlers of a single event cannot be controlled through the basic behavior of a built-in event itself. MulticastDelegates are “bags” of handlers, and they just grab them one at a time. Keep in mind that this is how most developers expect this to work, and it can be dangerous to allow order-dependent event handlers. Event handlers should normally not know about each other, because if they are dependent on being executed before or after another handler, they first have to know of the existence of the other handler (violating information hiding and several other design principles), and second, if that order changes, the behavior will be broken.

    If you understand all this, and still want to control the order of execution of handlers of an event, the following will get you close.

    1. Create an ordered collection of delegates of the event handler type called MyHandlers. This will be a surrogate for the actual event’s MulticastDelegate implementation.
    2. Create a “master” handler method that will actually be attached to the built-in event, and will iterate through MyHandlers and call each one.
    3. Define some means to add and remove handlers from the list. Some of this can be accomplished with a custom event “property”, but that will define only add and remove behaviors, not insert.

    The code might look like the following:

    private List<EventHandler> MyHandlers = new List<EventHandler>();
    
    private void MasterClickHandler(object sender, EventArgs e)
    {
       foreach(var handler in MyHandlers)
          handler(sender, e); 
    }
    
    public event EventHandler MyControlButtonClick
    {
       add { MyHandlers.Add(value); }
       remove { MyHandlers.Remove(value); }
    }
    
    public void InsertButtonClickHandler(EventHandler handler)
    {
       MyHandlers.Insert(handler,0); //calling this to add a handler puts the handler up front
    }
    
    ...
    
    myForm.MyControl.Click += MasterClickHandler;
    

    Notice that you’re no longer attaching handlers other than MasterClickHandler to the actual event; you can’t have your cake and eat it too, both overriding and keeping the basic event behavior. There also isn’t an “insert” behavior built into the event “property”; you have to define a method that allows this. Lastly, you should never raise the event MyControlButtonClick directly (though as your control is the only one that can, this can be enforced by code inspection).

    Now, when you click the button, the button’s built-in Click event fires MasterEventHandler, which will execute the delegates in MyHandlers in the same order they were attached to MyControlButtonClick (with any that were inserted executed first, in the reverse order they were inserted). If you placed this code in a custom user control with the Button, you could even name the custom event on your control Click, and the control would look and work much like the Button it contains, except that it would have the extra control over inserting handlers. The beauty of the whole thing is that nothing about this code forces consumers to work with it as anything other than a plain ol’ vanilla event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code utilize DOM Mutation Event DOMNodeInserted to detect the existence of the
How can I utilize System.out.print(ln/f) in a way that will allow me to format
How do I utilize fibers best in my game code? Should it only be
How to utilize ftp? Is there a generic dll that I can use for
Software Utilize C#, VS-2005 How to Remove Nagative Sign From Textbox. my code is:
I'm trying to utilize ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) to copy the current URL to the user's
I am trying to utilize Pinterest's pin it button on my website. The directions
I'm attempting to utilize some code found here: http://androidtabs.googlecode.com/svn/trunk/ This includes the bases classes
I am learning how to utilize the load event ..how do i go about
Sometimes code can utilize device drivers up to the point where the system is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.