views:

298

answers:

2

Hi,

I am currently building a prototype for a new system screen, and i am using c# to build this.

The question i have is, i currently have 14 textboxes which are filled from a condition from a couple of other controls on the screen. these 14 textboxes all add up to a total shown in another textbox.

as these textboxes are editable (in case the client wishes to increase the value) (cant go into to much detail but they will) I need to have a firable ontextchange event for when the values change so the total box updates.

however i have a feeling there must be a way of not having to create 14 different events, is there a way that i can have 1 event which fires if any of the 14 text boxes are fired?

thanks

Alan

+2  A: 

Yes, you can create a single event - and then subscribe all the text boxes to the same event handler in your class, which raises that single event.

If the clients will need to know which text box has changed, you'll need to think about how to best communicate that with them - you may want to create your own subclass of EventArgs. Otherwise, just a simple EventHandler or EventHandler<EventArgs> should work fine.

Jon Skeet
thanks this plus the comments below sorted the problem out =)
Alan Bennett
A: 

Yes, let's say your text boxes are called textBox1, textBox2, ... if you have automatically created method "textBox1_TextChanged" for event "TextChanged" of textBox1, you can easily use this method for all the other text boxes...

For example in Designer, just select yout "textBox2" component, go to Events tab on Properties Window and instead of creating new event by double clicking in the editable field next to the "TextChanged" value, just use the drop-down menu to select the "textBox1_TextChanged" method created (automatically) before.

If you need to decide which textBox raised the event, you can use the "sender" parameter of the method.

Ondra C.