C# Calling a method -
i have form multiple instances of user control on it.
i've assigned following:
switch.armyswitchclosebutton.click += armyswitchclose;
so when 1 of instances pressed, call following method:
void armyswitchclose(object sender, eventargs e)
the above method has bunch of additional code in isn't required here.
now need button, call above function every instance.
how can this?
many thanks
in constructor of each user control can pass same instance of object knows how run method void armyswitchclose(object sender, eventargs e)
then, call method inside each event method, ex:
public class usercontrol(){ private makeevent makeevent; public myclass(makeevent makeevent) { this.makeevent = makeevent; switch.armyswitchclosebutton.click += armyswitchclose; } void armyswitchclose(object sender, eventargs e) { makeevent.armyswitchclose(sender,e); } } public class makeevent() { void armyswitchclose(object sender, eventargs e) { //the real implementation } }
hope solve problem.
oh, think problem wrong... when click in 1 button other events in others uc must triggered, right? problem can solved observer pattern
public interface iobserver { void armyswitchclose(object sender, eventargs e); } public usercontrol1: observer { void armyswitchclose(object sender, eventargs e) { //implementation uc1 } } public usercontrol2: observer { void armyswitchclose(object sender, eventargs e) { //implementation uc2 } }
in button uc:
public usercontrolbutton { private list<iobserver> observers; public void addobserver(iobserver observer) { observers.add(observer); } public void button_clickedevent(object sender, eventargs e) { foreach(iobserver observer in observers) { observer.armyswitchclose(object sender, eventargs e); } } }
in form buttons call addobserver adding each user control.
Comments
Post a Comment