• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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?
0
andreas_rafn
Asked:
andreas_rafn
2 Solutions
 
DhaestCommented:
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.
0
 
Éric MoreauSenior .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
   Try
       ...
   Finally
      IsLoading = False
   End Try
end sub
0
 
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.
0
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now