473,574 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to remove event handler

I'm working in WPF and c# and am adding an event handler to an object like
this:

this.tgt.SizeCh anged += new SizeChangedEven tHandler(OnTarg etSizeChanged);

Later I kill the instance of tgt like this:

tgt = null;; //which I'm not sure this is the most correct way of getting
rid of it.

However, I find that the event 'OnTargetSizeCh anged' is still fireing.
Evedently the event handler is still alive and wired. Before I kill the
object 'tgt', should I be removing any attached event handlers? If so, how?

Thanks.

mo*******@noema il.noemail
Feb 27 '07 #1
8 9027
moondaddy <mo*******@noem ail.noemailwrot e:
I'm working in WPF and c# and am adding an event handler to an object like
this:

this.tgt.SizeCh anged += new SizeChangedEven tHandler(OnTarg etSizeChanged);

Later I kill the instance of tgt like this:

tgt = null;; //which I'm not sure this is the most correct way of getting
rid of it.
That's not "killing" anything. It's making the object available to the
garbage collector *if* nothing else is referencing it. Is it still a
control on the form, perhaps?
However, I find that the event 'OnTargetSizeCh anged' is still fireing.
Evedently the event handler is still alive and wired. Before I kill the
object 'tgt', should I be removing any attached event handlers? If
so, how?
Well, I think you need to seriously consider how you're "removing" tgt,
but to remove the event handler, just do:

this.tgt.SizeCh anged -= new SizeChangedEven tHandler
(OnTargetSizeCh anged);
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #2
Wouldn't that fail due to the SizeChangedEven tHandler being a different
instance than was added via +=?

Feb 27 '07 #3
Keith Patrick <ri************ *******@nospam. hotmail.comwrot e:
Wouldn't that fail due to the SizeChangedEven tHandler being a different
instance than was added via +=?
No. They'd still compare as being equal, which is what's used.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #4
Well, cool! I was gonna do the -= on some code a while back but saw the new
instance I had to create and didn't even check to see if it would work.
Guess I can finally fix that!

Feb 27 '07 #5
Thanks this looks like just what I need. By the way, this code is running
in a class that manages some custom bindings and tgt is a reference to a
user control on a canvas. when I connect a line to tgt, I use a custom
binding in this class. When tgt becomes un-connected I unbind it from the
line and no longer need a reference to tgt so I set tgt=null in the binding
management class. However, tgt is still alive and well in the canvas.
Saying all of this, is it appropriate to set tgt to null here? or should I
do something else?
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
moondaddy <mo*******@noem ail.noemailwrot e:
>I'm working in WPF and c# and am adding an event handler to an object
like
this:

this.tgt.SizeC hanged += new SizeChangedEven tHandler(OnTarg etSizeChanged);

Later I kill the instance of tgt like this:

tgt = null;; //which I'm not sure this is the most correct way of
getting
rid of it.

That's not "killing" anything. It's making the object available to the
garbage collector *if* nothing else is referencing it. Is it still a
control on the form, perhaps?
>However, I find that the event 'OnTargetSizeCh anged' is still fireing.
Evedently the event handler is still alive and wired. Before I kill the
object 'tgt', should I be removing any attached event handlers? If
so, how?

Well, I think you need to seriously consider how you're "removing" tgt,
but to remove the event handler, just do:

this.tgt.SizeCh anged -= new SizeChangedEven tHandler
(OnTargetSizeCh anged);
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 28 '07 #6
moondaddy <mo*******@noem ail.noemailwrot e:
Thanks this looks like just what I need. By the way, this code is running
in a class that manages some custom bindings and tgt is a reference to a
user control on a canvas. when I connect a line to tgt, I use a custom
binding in this class. When tgt becomes un-connected I unbind it from the
line and no longer need a reference to tgt so I set tgt=null in the binding
management class. However, tgt is still alive and well in the canvas.
Saying all of this, is it appropriate to set tgt to null here? or should I
do something else?
If you want the user control to actually "die" you'll need to remove it
from the canvas too. If you just want to not reference it any more from
tgt, then setting tgt to null is entirely appropriate.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #7
Great! Thanks alot.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
moondaddy <mo*******@noem ail.noemailwrot e:
>Thanks this looks like just what I need. By the way, this code is
running
in a class that manages some custom bindings and tgt is a reference to a
user control on a canvas. when I connect a line to tgt, I use a custom
binding in this class. When tgt becomes un-connected I unbind it from
the
line and no longer need a reference to tgt so I set tgt=null in the
binding
management class. However, tgt is still alive and well in the canvas.
Saying all of this, is it appropriate to set tgt to null here? or should
I
do something else?

If you want the user control to actually "die" you'll need to remove it
from the canvas too. If you just want to not reference it any more from
tgt, then setting tgt to null is entirely appropriate.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 28 '07 #8

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
moondaddy <mo*******@noem ail.noemailwrot e:
>Thanks this looks like just what I need. By the way, this code is
running
in a class that manages some custom bindings and tgt is a reference to a
user control on a canvas. when I connect a line to tgt, I use a custom
binding in this class. When tgt becomes un-connected I unbind it from
the
line and no longer need a reference to tgt so I set tgt=null in the
binding
management class. However, tgt is still alive and well in the canvas.
Saying all of this, is it appropriate to set tgt to null here? or should
I
do something else?

If you want the user control to actually "die" you'll need to remove it
from the canvas too. If you just want to not reference it any more from
tgt, then setting tgt to null is entirely appropriate.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 28 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
394
by: Chien Lau | last post by:
I have a form class that acts as a top level window. The user can open and close any number of these windows throughout the lifetime of the application. In the OnLoad, I have: Application.Idle+=new EventHandler(Application_Idle); ....and in the Dispose() override, I have: Application.Idle-=new EventHandler(Application_Idle);
1
5054
by: Daves | last post by:
I have a control defined: <asp:AccessDataSource ID="ADS1" OnUpdated="ADS_OnUpdated" > and in code behind: protected void OnUpdated(object sender, SqlDataSourceStatusEventArgs e) { ADS1.Updated -= new System.EventHandler(AD1_OnUpdated); }
3
7540
by: Franco, Gustavo | last post by:
How can I remove all event handler from one event without do -=? I won't explain why because is too long, and that the only option left I have right now. I have one event declared for: AppDomain.CurrentDomain.AssemblyResolve += XFUNCTION I need to remove all event handlers for the event AssemblyResolve in the
3
1615
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can one remove all event handlers attached?
1
329
by: Richard W | last post by:
I have a very simple web page (ASP.NET) that I am trying to build. On the web page is a checkbox that enables or disables other controls based upon the checked status. However, .NET fails to create the event handler for the CheckedChanged event. In addition, even if I manually create the event handler, the CheckedChanged event is never...
4
8704
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many handlers there are? <code> Public Class ClassA Private m_ClassB As New ClassB
10
12529
by: Franky | last post by:
How Remove All Handler on a event? Like remove all handler on the myButton.Click. Thanks ______________________________ Franky FrankyPDA_NOSPAM_@hotmail.com
2
2137
by: =?Utf-8?B?R3JlZ29yeSBLaHJh?= | last post by:
My code assigns event handler at runtime: /*ListView*/ lv.RetrieveVirtualItem += this.MyHandler; This line can be executed several times with the same list box and different handlers (to be exact, the same handler coming from different instances of an object). It appears from debugging that each execution adds a handler instead of...
4
7956
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take this. Here it goes class Product { public event EventHandler ProductChanged;
0
7813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8106
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3743
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2251
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1350
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.