Solved

500pts: Generic unsubscribing an event?

Posted on 2007-03-28
6
277 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

823 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