Re: How to reassign event handler at runtime

Tech-Archive recommends: Speed Up your PC by fixing your registry



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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Gregory Khra" <GregoryKhra@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:70CBB055-C36A-41E8-AE5C-69B7E9ACFF41@xxxxxxxxxxxxxxxx
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




.



Relevant Pages

  • Re: at_exit handlers and Process.kill
    ... execution when the program exits." ... If you put "sleep 1 while true" before ... handler will never be installed. ... See signalfor descriptions and numbers of the various signals. ...
    (comp.lang.ruby)
  • Re: How to use Switch Statement with RadioButtons
    ... Use a different handler for each button, where each handler has the button-specific code you desire. ... -- store the button control references in an array, using the index of each button as your "switchable" value, and simply enumerating the array to determine the correct index ) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: compile error about destructor
    ... the execution) and hence the execution hangs up. ... From MSDN, there are more values for exception code, check for the "Return ... the handler, should it skip the handler and continue ...
    (microsoft.public.vc.language)
  • Re: at_exit handlers and Process.kill
    ... execution when the program exits." ... If you put "sleep 1 while true" ... so the exit handler will never be installed. ... signals. ...
    (comp.lang.ruby)
  • Re: Trapping writes to mmapd memory.
    ... updates to the buffer _guaranteed_ to happen (assuming correct ... so that execution can be resumed as if nothing happened. ... the handler does not modify the saved state in *ctx, ...
    (comp.os.linux.development.system)