c# - How can I create a function that attaches multiple event handlers to multiple controls? -


question:
how can create function attaches multiple event handlers multiple controls?

intent:
using c# develop windows forms application. want create function takes collection of controls , collection of event handlers. function attach event handlers controls. elegant, , reusable way this. have pretty ugly way delegates, but less work me throw loop, , abandoning function.

behavior want:

foreach(control control in controlcollection)      foreach(eventhandler handler in eventhandlercollection)                     control.event += handler; 


function:
attachhandlers(? controlcollection, ? eventhandlers)

edit:
going subscribe handlers same event on controls. didn't explicitly in description, believe reason of confusion.

if controls in question inherit same base class or interface (or same class), can like:

void attachclickeventhandlers(list<iclickablecontrol> controls, list<myclickhandler> eventhandlers) {     foreach (var control in controls)         foreach (myclickhandler handler in eventhandlers)             control.click += handler; } 

this assumes interface like:

public interface iclickablecontrol {     event myclickhandler click; } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -