C# 用户控件工厂
假设您有两个扩展UserControl
的类。每个控件都提供一个自定义事件
(这可以通过使用接口来完成)。
您希望在奇数天显示其中一个控件,在偶数天显示另一个控件。
您还希望能够将 UserControl
拖放到表单上 (Visual Studio),而无需知道 Control 类型最终是什么。
你是怎么做到的?工厂模式在这里有用吗?
Let's say you have two classes that extend UserControl
. Each of the controls provides a custom event
(this could be done by using an interface).
You want to display one of the controls in the odd days and the other in the even days.
You also want to be able to drag&drop (Visual Studio) the UserControl
on your form without knowing what the Control type will finally be.
How do you do that ? Is the factory pattern useful here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个添加到表单上的容器控件(并且存在于设计器工具箱中),该控件在内部使用一些工厂来创建要使用的实际控件的实例,然后使用 Dock 将其添加到容器中 设置为
填充
。I would make a container control that is added on the form (and that is present in the designer toolbox), that internally uses some factory to create an instance of the actual control to use and then adds it to the container with
Dock
set toFill
.您可以创建第三个用户控件来创建 &根据日期托管用户控件。
但是,这有一种不好的感觉,你能更详细地解释一下你实际上想要做什么吗?
You could make a third usercontrol that creates & hosts the usercontrol depending on the day.
But, this has a bad feeling to it, could you explain more in detail what you actually are trying to do?