Solved

500pts: Generic unsubscribing an event?

Posted on 2007-03-28
6
281 Views
Last Modified: 2008-02-01
Hi there,

I wonder if anyone can help, i need to unsubscribe from an event but i don't know the method at runtime.. Probably better if i explain with an example..

myGraphics.Click += new EventHandler(myGraphics_Click); // this is set in a form of some kind..

I then pass around myGraphics to difference forms so i need to reset the .Click event to myGraphics_Click but the problem is that it is still set to myGraphics_click on the original form before it was passed so hence the event fires on both forms :-)

Is there a way i can NULL the click event or something, or iterate through all subscribers to the click event so i can decide how to unsubscribe..

The problem is that i will probably have to unsubscribe the original form when i have arrived in my new form...

Confused? I am :-) .... Any ideas how to do this... really apprecaite any help

thanks in advance

0
Comment
Question by:ianinspain
6 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18809919
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 200 total points
ID: 18810231
Just remember the EventHandler that you added, then remove the same instance.

private EventHandler ClickEventHandler;

when you subscribe ...

ClickEventHandler = new EventHandler(myGraphics_Click);
myGraphics.Clicked += ClickEventHandler;



to unsubscribe ...
myGraphics.Clicked -= ClickEventHandler;


Cheers,

Greg
0
 

Author Comment

by:ianinspain
ID: 18814381
Thanks greg! i sort of have it working...

I had to make the event a static event because when the ovject with the click evetn arrives in another form I don't have a reference to it ... so i technically can't do myGraphics.Clicked -= ClickEventHandler;  or it would try to unsubscribe in the current form whcih i haven't subscribed to yet...  I presume i am correct on this?

Is there anyway i can check the object to see if anything has subscribed.... as i was getting a double event somewhere in my code.... so if i could do

if myGraphics.clicked == OriginalForm.ClickEventHandler
{
// hass been subscribed in original form
// then unsubscribe....

// then subscribe in this form

}
else
{

// jsut subscribe in this form..
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 150 total points
ID: 18814445
You can only do those sorts of tests from within the myGraphics class itself. Anything outside the class can only ever do += or -= to an event 'variable'.

How about doing if ( OriginalForm != null ) { ... unsubscribe} rather than checking the handler...

Alternatively, it's perfectly OK to detach events that have never been attached... (it'll just do nothing). So myGraphics.clicked -= OriginalForm.ClickEventHandler is OK even if you never did a += beforehand.
0
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 150 total points
ID: 18816015
If myGraphics it is your class you can check it inside
if (Clicked != null)
{
.....
}
and create the public function
0
 

Author Comment

by:ianinspain
ID: 18835025
Thanks.. no it isn't the class... but thanks everyone for the input.... i am going to split the points...
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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