开发者

Windows forms: Wiring up custom events between controls added at runtime

If I have the following form:

Screenshot here

The form has multiple usercontrols (orange) wich also contains multiple usercontrols (green).

My questions are:

1) If I add an extra 'green' usercontrol to one of the 'orange' usercontrols, what is the best way to wire up this new 'green' usercontrol to an event of the Button on the main form (without the 'orange' or 'green' usercontrol knowing about the main form)?

I thought firing an event from the 'orange' usercontrol to the main form with the 'green' usercontrol as a parameter, and then handling the wiring in the main form.

2)Are there beter 'places' to handle this wiring?

3) If all the 'green' usercontrols need to wire up to event of eachother (for example if a value changes in one of them, all others need to be inform开发者_JAVA技巧ed), what is the best way? I thought about creating a singleton class EventManager to handle this?

I'm really struggling with the combination of loosly coupled usercontrols and wiring them up.

I hope my question is clear.

Thanks in advance, Kevin


This is the way I think about it, any control/class should be responsible for configuring itself and the public interface of its immediate children. The user of a class or control should not need any knowledge of it parent or any internal knowledge of its children.

So, in your example, the orange controls should subscribe to the green controls events and act accordingly, by perhaps raising thier own event to the parent and or maybe effecting some field on a sibling green control. When green controls are added and removed from the orange, the orange control is responsible for maintaining its internal "wiring." This is true whether this happened in a public or private method on orange, if the host is capable of doing this through some exposed collection on orange the logic for doing this must be encapsulated in orange.

This statement is equally true, whether we consider orange, the host, green or any other class/control.

In terms of coding you'll want to use the += and -= with the Multicast Delegate\Event subcriber model when controls are added and removed. The handlers will be coded appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜