If you want to keep it supported, I would do something like this:
//btnArray can also be populated by traversing relevant Controls collection(s) Button[] btnArray = new Button[] {button1, button2}; foreach(Button btn in btnArray) { btn.Click += button_Click; }
Any refactoring for this code is done through one service point. For example:
- You decide to associate custom event buttons with a custom class, i.e. MyClick.
- You decided to change the logic of how buttons are assembled, added or removed buttons.
- You see that button_Click is a bad name or needs to be bound to a conditional event.
Neolisk
source share