Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Some simple questions

I have several objects on my form. Radio buttons, checkboxes, datafields - you name it.

I have around 25 of them.

When some value changes in **any** of them, i want to call one function.

Now, i know that under the event handler for each item, i can put in that code. But dont want to do it 25 times.

Is there an elegant solution to this problem?

Sunday, December 19, 2004

What about assigning the same event handler to each of the controls?

Sunday, December 19, 2004

Are you doing VB .NET?  Just create the procedure for one of the controls under the appropriate event, and then add to the Handles clause the other 24 controls with their appropriate events.

(And if there's no appropriate event, you could inherit from the existing controls and create your own...)

Your function will look something like

Private Sub MyFunction(sender as Object, e as EventArgs) Handles Button1.Click, Button2.Click, Button3.Click (etc.)

Note that you can handle a given event, such as Button1.Click, in as many procedures as you want; just don't count on them being handled in a certain order.

Note also that some events require a different signature (usually a different class in place of EventArgs) so you might still have to write more than one handler function.

Sunday, December 19, 2004

Hey there,

a.  Define a common event delegate
b.  Ideal would be to have a routine, which will iterate through *all* the controls in the form (the 'controls' property) and add the event delegate to each Control's TextChanged event, hence all / *most* of the UI controls are derived from Systems.Windows.Forms.Control
c.  Also, to write code to remove the event delegate from the controls would be easy, if in case you decide that u'd not need the notifications after a certain stage.


Monday, December 20, 2004

*  Recent Topics

*  Fog Creek Home