VB.NET Event handling subs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I need to "save" in a variable the event handler sub of a control's
event, then perform some process, and finally "restore" the originally saved
event handler.

Example in pseudo-code:

1) Save cmbMyCombo's event handler for SelectedIndexChange event.

2) Assign a temporary event handler sub to cmbMyCombo's for its
SelectedIndexChange event.
AddHandler cmbMyCombo.SelectedIndexChanged, AddressOf
cmbMyCombo_SelectedIndexChanged_Temporary

3) Do some processing...

4) Restore the previously-saved event handler sub for the
SelectedIndexChange event to cmbMyCombo.

How can this be accomplished? Thanks in advance.

Regards,

Rick
 
What are you really trying to accomplish? Why does it matter if the event
handler is changed during the processing of the event - it's not going to
have a chance to fire at this point anyway.
 
Thanks for your help. I'm writing a generic method that receives a combox as
a parameter, temporarily removes or replaces one of its event handlers, and
in the Finally clause, as part of the cleanup, needs to restore the original
event handler so the combobox continue working as before. In C/C++ it'd be
down to saving the address of the subroutine as a pointer, but how can this
be done in VB.NET? RemoveHandler doesn't return anything... Thanks in advance.

Regards,

Rick
 
I still don't understand why you need to temporarily change what the event
handler is.

If in the handler for event A, you don't want event B to fire, then I don't
think getting rid of the handler is the way to go. Because if the user can
still interact with the control, then that is the real problem. If the user
can change the selected index and just not have the event fire, after all is
said and done the user will have changed the value of the dropdown, but no
event would have fired. Because no event fired, the handler didn't run, and
now your form is in an inconsistent state.

Again I ask what is it you are trying to accomplish - in the bigger sense.
What functionality are you trying to give your application? I understand the
mechanics you are trying to achieve.

I am not sure if there is a way to get the invocation list for a given
object's event. That makes sense to me, because that list should be private
to the object itself, and something using the object shouldn't just be able
to get a list of all the listeners for a particular event.
 
Richard,
In addition to the other comments:

| Hi, I need to "save" in a variable the event handler sub of a control's
| event, then perform some process, and finally "restore" the originally
saved
| event handler.
There may be multiple event handler subs for a single events, is there a
specific handler you want to save or do you want to save the entire list?

The "problem" is that the list of event handlers are not publicly available.
Different classes store the list differently. A number of framework classes
store the handlers in the Component.Events member:

http://msdn2.microsoft.com/en-us/library/xe4ht2sc(en-US,VS.80).aspx

While the convention for VB classes is a hidden field named for the Event,
for example the MyValueChanged event keeps the list in the
MyValueChangeEvent field.

VB 2005 can use Custom Events to store the list of event handlers in the
Component.Events member.

http://www.panopticoncentral.net/archive/2004/08/06/1545.aspx

Gaining access to this list for classes other then your own, is problematic
at best, in that how the list is stored is an implementation detail that
could change...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi, I need to "save" in a variable the event handler sub of a control's
| event, then perform some process, and finally "restore" the originally
saved
| event handler.
|
| Example in pseudo-code:
|
| 1) Save cmbMyCombo's event handler for SelectedIndexChange event.
|
| 2) Assign a temporary event handler sub to cmbMyCombo's for its
| SelectedIndexChange event.
| AddHandler cmbMyCombo.SelectedIndexChanged, AddressOf
| cmbMyCombo_SelectedIndexChanged_Temporary
|
| 3) Do some processing...
|
| 4) Restore the previously-saved event handler sub for the
| SelectedIndexChange event to cmbMyCombo.
|
| How can this be accomplished? Thanks in advance.
|
| Regards,
|
| Rick
 
Thank you for the explanation and the links. The combobox in particular
doesn't expose its list of event handlers, so there seems to be no simple way
to accomplish that in VB.NET

Regards,

Rick
 
Back
Top