Please see my comment to the question. See also:
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself[
^].
The question looks extremely trivial, but, come to think about, it is not. Here is why. Your problem is not that it would be difficult to write one universal function. The problem is that you have all those
CheckBox2
and
TextBox4
in principle. The problem is that you already created them
in a way too manual way — using the designer. It was manual clicking many times. You see, your code shows that
CheckBox2
is in certain
relationship with
TextBox2
. But what is the relationship between
CheckBox2
and some other control? There is no a way to calculate it automatically, because all you have are only
ad-hoc names of controls, which means nothing.
Of course, you can abstract out these two controls to make then two parameters of some function. But anyway, you will have to write those 20 event handlers and call this function 20 times from those handlers, which would make only one line of the implementation code of each handler. Maybe you would be happy with such advice, but I would be disgusted with it.
Your problem that you already gone wrong with the form. I would redo it. Just the fact you have
names line
CheckBox2
and
TextBox4
is a good indicator of wrong code design (you should never use auto-generated names and rename all controls using refactoring language, to give semantic name and observe good Microsoft naming conventions, but it cannot help you in this case). You have to have arrays of controls. First idea is to create array on check boxes and another array, of text boxes, and then your code would use the the arrays elements accessed by index, something like if
checkBox(index).Value then texBox(index)…
but then you would write some code populating the arrays with the control members of the form. At least you can do it only once.
But it also would be not so good. Why would you even need those members, if you really need only one array for all those check boxes (maybe, not all of them, but those in the similar relationships with the elements of the array of text boxes), and only one array of those text boxes, and so on? The designer won't create those arrays for your?
The answer is: design properly. Perhaps you don't need that many check boxes (it already sounds suspicious, maybe you can redesign it), but let's say this is a reasonable design, so you really need 20 check boxes and related text boxes. As the designer won't do right thing, don't use the designer.
You need to create all those controls in code, in a loop. This is the key. And add only one handler of each event instance to each of those controls, in a loop. You only need to pass the index to the handler method. The handler methods takes only two parameters, sender (
System.Object
) and event arguments (
System.EventArgs
or derived class). One possible technique is this: create regular values of
TabIndex
for all controls. Then you can cast
sender
to correct control type (which you know in the code of the handler), and then calculate the array index out of
TabIndex
. Alternatively, you could use
anonymous methods for handler and
closure (
https://en.wikipedia.org/wiki/Closure_%28computer_programming%29[
^]).
—SA