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 3851024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:05:57+00:00 2026-05-19T17:05:57+00:00

I have some class (a user control) MyUserControl that rises events. I use it

  • 0

I have some class (a user control) MyUserControl that rises events.

I use it in a form MyForm, in that, for some reasons I am forced to use AddHandler instead of WithEvents<>Handles pair.

I use AddHandlers in the MyForm_Load method.

Now. Adding handler is OK, the question now is where to remove this handler. Should it be done in the Finalize method of MyForm? à la

Protected Overrides Sub Finalize()
  RemoveHandler _myCtrl.Action1Performed, AddressOf MyControl_Action1Performed  
  RemoveHandler _myCtrl.Action2Performed, AddressOf MyControl_Action2Performed  
  RemoveHandler _myCtrl.Action3Performed, AddressOf MyControl_Action3Performed  
End Sub
  • 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-19T17:05:57+00:00Added an answer on May 19, 2026 at 5:05 pm

    When the container control (such as your form) is disposed, all of its event handlers will be automatically removed and disposed. This cleanup happens for you automatically because neither your user control nor its container form exist anymore.

    The only time you really need to worry about calling RemoveHandler is if you’re hooking up event handlers to objects that are expected to have a significantly longer lifespan than their containers. In that case, it’s possible to create a memory leak because that object cannot be garbage collected as long as that handler is subscribed. You can read a much more comprehensive case study here on Tess Ferrandez’s blog. This is not a problem if both objects go “out of scope” at the same time.

    EDIT: If you still feel that you absolutely need to remove the handler (perhaps you’re obsessive compulsive?), you can go ahead and do it in the form’s Dispose method. A purist might balk at the use of IDisposable for something like this, but you won’t see any ill results. You do want to make sure that you don’t implement the Finalize method for this, though. There’s just no point: the finalizer method won’t get called until there are no more strong references to the object, but the only possible harm in failing to call RemoveHandler is that a container will hold onto a reference to that object longer than necessary. You’re fooling yourself by trying to remove event handlers in the Finalize method.

    Also keep in mind that it really doesn’t matter where exactly you do it, because the purpose of AddHandler/RemoveHandler is to allow you to add and remove event handlers dynamically. You’re allowed to call them anywhere in your code that you want.

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

Sidebar

Related Questions

I have some logic, which defines and uses some user-defined types, like these: class
I have some event handler on a boundary class that manages a persistence mechanism
I have some code that will be accessed from two threads: class Timer{ public:
Here's the problem....I have three components...A Page that contains a User Control and a
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
What exactly are the Python scoping rules? If I have some code: code1 class
I have some classes layed out like this class A { public virtual void
I have some ASP.NET web services which all share a common helper class they
Say I have some code like namespace Portal { public class Author { public
Let's say I have some Java code: public class SomeClass { static { private

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.