Trapping all events

raju_shrestha

Newcomer
Joined
Nov 23, 2003
Messages
14
Is there anyway to trap all events to have centralized control?

Let me try to make the problem clear. Suppose I have a form and several many controls on it. As we know every control has its own events and according to certain actions(by user, system etc.), corresponding event gets fired. I am trying to centralize the event handling to handle all events of the form and containing controls in one procedure something like:
Sub EventFired(source As Object, event As ???, args As EventArgs)
' source identifies the control say source.Name gives BtnOk, event should give the fired event, say BtnOk.Click o r just Click, args gives the arguments for the event
.....
..... code to identify the source of event, event type and event arguments and can control whether to let the event gets fired or cancel
.....
If (condition1) Then
args.Cancel = True ' cancels the event being fired
End If
End Sub

Can this be done? If so how? Appreciate for the ideas, comments and solutions.

Thanks
 
It can be done, but what comes to mind is

you would need to register for every event in the Control.

You would have to have several event handlers because the delegates of some of the events have different signatures.

But I think it would be a problem because you would have to upcast stuff fire 'EventFired'

and inside the method that handles 'EventFired' you would need to do type checking and casting.

It would be a lot of code, and I don't think it would be worth it because of the type checking and casting.


e.x.:
OK, so lets say 'EventFired' was raised

inside my method I would need to determine
what Type threw the event.
What type of event was it
What type of eventargs do I have to work with/can they event be canceled.
 
That solution was in my mind too. But its not practicable to register all events. I agree that there needs to be some work to find type of object, type of event. And my point is the event must be able to cancelled if needed. For all these, I haven't got .Net solution so far so putting this in the forum for the answer from the experts. I suspect, if no .Net solution, may need to use low level api calls. But if it can be done, it will really help a lot in some situations as we will have full control over the events to fire, cancel, redirect etc according to situations and needs.
 
ya, it can be done

but you will need to listen to every event you want the 'EventFired' event to raise.

The Form class has about 50~60 events.

And for the second parameter of 'EventFired'
it would probably have to be from an enumeration

e.x.:

enum Events {Click, Close, Open, Maximize};
 
Back
Top