An "All the time" event?

  • Thread starter Thread starter Lars Netzel
  • Start date Start date
L

Lars Netzel

Is there any way to add some code that will happen in every single Event
that i raised in a form?

I have a Total field in the bottom of a pretty complex page and I think it's
kind of "Dirty" to update that Filed from every single Event that might
affect it

Is there are was to keep that Filed Total (a value) updated at all time
without having to think about it in every single event on the page?

Nomatter what happens and what fileds are updated on the page.. in a grid or
in another filed, I want that Total filed to be Updated...

/Lars
 
Nope.

couple reasons.

1) Thats a lot of friggin events

2) Each event has its own signature. While many are the same, there are
many that are different. No "universal" event handler.

3) Not that many events fire on an ASP.NET page at once... Usually, 1...

4) If you want to catch something each time it happens in ASP.NET the
Page.Load event is always the first one fired, and you can validate whether
its a postback or not.

5) Look into storing values in the viewstate... or the session object.

hope it helps.

-CJ
 
You could try hooking up all your controls Leave Events or Validating Events
(if you aren't using them for something else) to one event handler and call
your update form there.
 
Lars,
In addition to CJ's comments.

Have you separated your UI from your Domain/Data objects? This should be
easy! In your domain objects as properties change, the Total property would
change. If you don't have Domain (business) objects per se and are binding
directly to Data objects (such as a DataSet/DataTable) you can use the
events on the DataTable itself to update to total fields. Such as
DataTable.ColumnChanged, DataTable.RowChanged and DataTable.RowDeleted.

An alternative:
Remember that an Event can be handled by more then one handler, and that a
single handler can handle events for more then one object.

The "problem" as CJ suggested, is that the signature of the event handler
needs to match the signature of the event. You may need two extra
handlers...

You could have an extra Changed event handler that would update the total
field, for each of the UI controls, they would have their own Changed event
handler, plus this extra Changed handler would handle the changed event.

Private Sub Text1_Changed(...) Handles Text1.Changed

Private Sub Text2_Changed(...) Handles Text2.Changed

Private Sub Text3_Changed(...) Handles Text3.Changed

Private Sub AnyText_Changed(...) Handles Text1.Changed, Text2.Change,
Text3.Changed
' calculate total amount
End Sub

You can use VS.NET to set the first three Changed event handlers, however
you will need to type in the third event handler.

Hope this helps
Jay
 
Lars,
I should add, rather then using the Handles syntax to add the event handlers
on the Changed event handler you could use a For Each loop over the Controls
collection and use AddHandler to add the extra event handler.

Private Sub Form1_Load(...) Handles MyBase.Load
For Each ctl As Control In Controls
If TypeOf ctl Is TextBox Then
AddHandler DirectCast(ctl, TextBox).Changed, AddressOf
AnyText_Changed
ElseIf TypeOf ...
...
End IF
Next
End Sub

Note: if you have panels & other controls that may be nested, you may need
to make the above recursive on ctl.Controls!

Hope this helps
Jay
 
Back
Top