Solved

500pts: Generic unsubscribing an event?

Posted on 2007-03-28
6
264 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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