473,811 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reassign event handler at runtime

My code assigns event handler at runtime:
/*ListView*/ lv.RetrieveVirt ualItem += 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 2147
Gregory,

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

// Store this somewhere.
RetrieveVirtual ItemEventHandle r oldHandler = ...;

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

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

"Gregory Khra" <Gr*********@di scussions.micro soft.comwrote in message
news:70******** *************** ***********@mic rosoft.com...
My code assigns event handler at runtime:
/*ListView*/ lv.RetrieveVirt ualItem += 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*********@di scussions.micro soft.comwrote in message
news:70******** *************** ***********@mic rosoft.com...
My code assigns event handler at runtime:
/*ListView*/ lv.RetrieveVirt ualItem += 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.RetrieveVirt ualItem -= 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
7051
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
26519
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
3524
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 following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
2
1769
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 runtime ? I try to use 1. me.Events.Item(Button1) . This will return Nothing 2. me.Events.Item(Button1.Click). This will throw out an exception saying 'Click' is not a member of 'Button'
3
3995
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 about the control they just entered (sort of like an extended tooltip). Now, I can certainly create a separate mouseenter event for each control (too much work, and not very clever), but what I would like to do is somehow create one event that would...
5
1343
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 won't let me set it to the address of my own handler. Do I have to inherit every control just because I want to create it dynamically at runtime?
2
2662
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
1603
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 / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change. The problem is that I...
2
3058
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 / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change.
0
9605
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10133
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
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
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.