Solved

500pts: Generic unsubscribing an event?

Posted on 2007-03-28
6
270 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
coded character set iptc field within a jpeg 1 69
Remove greater than sign 3 44
How can my static class become undefined?? 8 13
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

12 Experts available now in Live!

Get 1:1 Help Now