Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

500pts: Generic unsubscribing an event?

Posted on 2007-03-28
6
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18809919
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 600 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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 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