Trapping all events

  • Thread starter Thread starter Raju Shrestha
  • Start date Start date
R

Raju Shrestha

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 its 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

Raju Shrestha
 
I'm not aware of a "magical" line of code that could do this. But you can
always attach an event handler manually to any event, using the AddHandler
statement in VB.NET. You could use for all events the same handler (e.g. sub
EventFired).

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
* Raju Shrestha said:
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 its 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

Maybe with reflection + 'AddHandler'.
 
Back
Top