c# - How a Winform custom control can notify another Winform Custom Control? -
let's in custom control defined custom event myevent. when raising event, event captured parent form.
how can capture event in custom control on same parent form ? i'd other control subscribe first control event somehow.
i've run similar situation lot when dealing mvc. way handle use mediator design pattern in controller.
basically, have class has register function , notify function. register function takes object implements listener interface , messageid. stores these in dictionary. notify function takes messageid event needs sent listeners , notifies appropriate ones event has occurred.
so maybe along lines of
public interface ilistener { void messageraised(int messageid, params object[] arguments); } public class mediator { public void register(ilistener listener, int messageid) { //... add dictionary of arrays each messageid } public void notifylisteners(int messageid, params object[] arguments) { //... loop through listeners messageid , call messageraised function each 1 } }
now have base controller , implements static mediator object. other controllers inherit it. if using code behind , cannot inherit, might try using singleton pattern. .net static classes pretty great since have constructor use well.
so in case, have code behind each control implement ilistener , in constructor each one, have mediator.getinstance().register(this, messages.myevent). kind of quick , dirty way can refactored bit in future make bit more reusable.
some resources quick google search
http://www.avajava.com/tutorials/lessons/mediator-pattern.html
http://sourcemaking.com/design_patterns/mediator
good luck
Comments
Post a Comment