What is the easiest way to dynamically disable events for a Control in VB.NET?

I need to disable events for a control in order to avoid recursion. What is the easiest way to dynamically disable events for a control (or other class with events) in VB.NET?

I'm familiar with the AddHandler and RemoveHandler implementation, but I was hoping for something a little less cumbersome. Something like the Application.EnableEvents method of VBA.

Any suggestions?
Who is Participating?
DhaestConnect With a Mentor Commented:
I would suggest setting a boolean variable to false before your operation and resetting it after. Test this variable in the the event handler and only do something if it is true.
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
I also use the suggestion from Dhaest. For example, I have a IsLoading property/variable and in some methods I have this kind of code

private sub X()
   If IsLoading then Return
   IsLoading = True
      IsLoading = False
   End Try
end sub
andreas_rafnAuthor Commented:
I guess thats a effortless, but effective way of doing it :) I must admit however that I had hoped for something like Form.DisableEvents = True to exist in .NET.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.