?
Solved

check if an event is hooked?

Posted on 2008-10-07
6
Medium Priority
?
780 Views
Last Modified: 2013-11-26
Hi experts

I have a BindingSource which has the event CurrentChanged.

Now have to hook/unhook this event dynamically in my code. This alone wouldn't be a problem.
But how can I check if the CurrentChanged event is hooked up?

I tried

if(this.MyBindingSource.CurrentChanged == null)
{

}

but this gives an error which says that I can use CurrentChanged only with += or -=
I'm using .NET 2.0, VS2008, WinForms

Thanks
0
Comment
Question by:Arikael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22658448
Not usre exactly but take a look at

this.Events[eventname].GetInvocationList().Length
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22658451
Sample events:

this.Init += new EventHandler(Default3_Init); // to add
this.Init -= new EventHandler(Default3_Init); // to remove
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22658456
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22658460
you can further check
if (this.Init != null) { .. event is hooked }  else { event is not hooked .. }
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 2000 total points
ID: 22658514
I tried your code and got the same exception (not with binding source but with normal button control)
i am assuming that the inbuilt microsoft controls dont allow you from checking whether the event has been subscribed to or not

in this case i will recommend you to use a variable holding how many events have been hooked to and when ever a new event is hooked the value will be incremented and vice versa
0
 
LVL 3

Author Comment

by:Arikael
ID: 22658563
@crazyman

your code gives me the same exception as noted in my question.
it seems that you can't check whether the event has been subscribed to or not (see ragi0017's post).

Until now I just "unhooked" my event before I hooked it.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question