Universal Event Handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hartley_aaron@hotmail.com

    #1

    Universal Event Handler

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    objMyObject.Ena ble = False
    'do something
    objMyObject.Ena ble = True
    End Sub

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    objMyObject.Ena ble = False
    'do something else
    objMyObject.Ena ble = True
    End Sub

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    objMyObject.Ena ble = False
    'etc
    objMyObject.Ena ble = True
    End Sub




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


    Private Sub UniversalEventH andler(ByVal objEvent As EventHandler,
    ByVal sender As System.Object, ByVal e As System.EventArg s) Handles
    MyBase.All
    objMyObject.Ena ble = False
    objEvent.Invoke (sender, e)
    objMyObject.Ena ble = True
    End Sub

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

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

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    'etc
    End Sub


    Does anyone have any ideas?

  • Chris

    #2
    Re: Universal Event Handler

    hartley_aaron@h otmail.com wrote:[color=blue]
    > 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(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button1.Click
    > objMyObject.Ena ble = False
    > 'do something
    > objMyObject.Ena ble = True
    > End Sub
    >
    > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button2.Click
    > objMyObject.Ena ble = False
    > 'do something else
    > objMyObject.Ena ble = True
    > End Sub
    >
    > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button3.Click
    > objMyObject.Ena ble = False
    > 'etc
    > objMyObject.Ena ble = True
    > End Sub
    >
    >
    >
    >
    > Maybe its just wishful thinking, but what I would like to have is
    > something like this (obviously wrong):
    >
    >
    > Private Sub UniversalEventH andler(ByVal objEvent As EventHandler,
    > ByVal sender As System.Object, ByVal e As System.EventArg s) Handles
    > MyBase.All
    > objMyObject.Ena ble = False
    > objEvent.Invoke (sender, e)
    > objMyObject.Ena ble = True
    > End Sub
    >
    > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button1.Click
    > 'do something
    > End Sub
    >
    > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button2.Click
    > 'do something else
    > End Sub
    >
    > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button3.Click
    > 'etc
    > End Sub
    >
    >
    > Does anyone have any ideas?
    >[/color]

    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

    Comment

    Working...