473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to reassign event handler at runtime

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 overwriting it. As a result event causes the execution of all old
handlers as well as the new one.
Am I right? If yes, how can I remove the old handlers?
Gregory
Sep 13 '07 #1
2 2131
Gregory,

You will have to hold a reference to the old handler, and then remove
it:

// Store this somewhere.
RetrieveVirtualItemEventHandler oldHandler = ...;

// When adding the event, remove the old handler.
lv.RetrieveVirtualItem -= oldHandler;

// Add the new handler.
lv.RetrieveVirtualItem += this.MyHandler;
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gregory Khra" <Gr*********@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
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 overwriting it. As a result event causes the execution of all
old
handlers as well as the new one.
Am I right? If yes, how can I remove the old handlers?
Gregory


Sep 13 '07 #2
Hi,

"Gregory Khra" <Gr*********@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
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 overwriting it. As a result event causes the execution of all
old
handlers as well as the new one.
Am I right?
Yes, that is the way it works.

You have to call lv.RetrieveVirtualItem -= this.MyHandler; to remove the
handler.
Sep 13 '07 #3

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
2
by: Fei | last post by:
HI, I have a problem about EventHandler. I have a form with one button on it. I implement Button's click event. Is there a way I can determine whether I implement a handler for this event at...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
5
by: Water Cooler v2 | last post by:
I am creating a LinkLabel control at runtime. How do I attach an event handler to it? Where's the delegate for the OnClick event? I see an OnLinkClicked delegate but since it is protected, it...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton /...
2
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton /...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.