c# - Button Click Event Generalization -
what can loop through buttons on form programmatically , add event handlers each one.
put in form's constructor:
foreach (var ctrl in this.controls) { if (ctrl button) { ((button) ctrl).click += mainform_click; } }
and here's event handler:
void mainform_click(object sender, eventargs e) { listbox1.items.add(((button) sender).text); }
equalsk's suggestion one: if have other buttons on form don't wish part of behavior, can put buttons do want in panel or other control on form. you'd change foreach
in constructor this:
foreach (var ctrl in this.panel1.controls)
Comments
Post a Comment