Solved

check if an event is hooked?

Posted on 2008-10-07
6
741 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now