Universal Event Handler

H

hartley_aaron

Hi,

I was wondering if it were possible to add some code that would proceed
and/or follow the execution of each each event in the form and/or
application? The code would be the same no matter which event is being
called, so it would be ideal be able to write it only once, or at least
once for each form as opposed to writing it into each event in the
application.

In my situation, I need to deactivate a particular global class object
whenever an event is started and activate it again when event handling
code completes. In other words, I need to have the object turned off
when anything is happening.

As far as I know currently I would have to add the code to each every
event in my code and maintain that over time. For example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
objMyObject.Enable = False
'do something
objMyObject.Enable = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
objMyObject.Enable = False
'do something else
objMyObject.Enable = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
objMyObject.Enable = False
'etc
objMyObject.Enable = True
End Sub




Maybe its just wishful thinking, but what I would like to have is
something like this (obviously wrong):


Private Sub UniversalEventHandler(ByVal objEvent As EventHandler,
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.All
objMyObject.Enable = False
objEvent.Invoke(sender, e)
objMyObject.Enable = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'do something
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'do something else
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
'etc
End Sub


Does anyone have any ideas?
 
C

Chris

Hi,

I was wondering if it were possible to add some code that would proceed
and/or follow the execution of each each event in the form and/or
application? The code would be the same no matter which event is being
called, so it would be ideal be able to write it only once, or at least
once for each form as opposed to writing it into each event in the
application.

In my situation, I need to deactivate a particular global class object
whenever an event is started and activate it again when event handling
code completes. In other words, I need to have the object turned off
when anything is happening.

As far as I know currently I would have to add the code to each every
event in my code and maintain that over time. For example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
objMyObject.Enable = False
'do something
objMyObject.Enable = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
objMyObject.Enable = False
'do something else
objMyObject.Enable = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
objMyObject.Enable = False
'etc
objMyObject.Enable = True
End Sub




Maybe its just wishful thinking, but what I would like to have is
something like this (obviously wrong):


Private Sub UniversalEventHandler(ByVal objEvent As EventHandler,
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.All
objMyObject.Enable = False
objEvent.Invoke(sender, e)
objMyObject.Enable = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'do something
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'do something else
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
'etc
End Sub


Does anyone have any ideas?

I have an idea for you, haven't tried it but the theory should work....

Make your own button class, in that class override the OnClick event.
If you don't call the MyBase.OnClick() the event will never fire. Then
make a static variable in the class call "Don't Fire Events" Only call
the mybase.onclick if the "Don't Fire Events" variable is not set.

Hope this helps.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top