c# - Button Click Event Generalization -


how text of button, when click it, in listbox without defining event button. how generalize it.

enter image description here

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

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -