【发布时间】:2017-06-23 15:59:52
【问题描述】:
假设我的应用程序有大约 300 个不同形式的标准控件。 总有一天,给所有控件额外的成员会很好。
必须做什么?我的想法是:
从这样的基类派生Control 的每种类型:
public partial class MyButton : Button, MyInterface
{
...
}
public partial class MyTextBox : TextBox, MyInterface
{
...
}
public interface MyInterface
{
// Additional members
...
}
这意味着触摸每一个 Control 来改变它
private System.Windows.Forms.Button myButton;
private System.Windows.Forms.TextBox myTextBox;
到
private MyButton myButton;
private MyTextBox myTextBox;
从
this.myButton = new System.Windows.Forms.Button();
this.myTextBox = new System.Windows.Forms.TextBox();
到
this.myButton = new MyButton();
this.myTextBox = new MyTextBox();
我的问题:有没有更简单的方法?如果可能的话,也许用另外派生自MyInterface 的类替换Control 类? (Control.Tag 属性是不可选项)
【问题讨论】:
-
您要向控件添加什么样的属性?
-
@Dispersia:e。 G。一个列表
-
好的,但是为什么要在控件上使用这些?控件用于用户输入,它们不应包含数据。
-
虽然用派生控件替换这些控件似乎只是一个简单的 Find/Replace 操作,但我也分享了一个不错的选项,在某些情况下您可能会发现它很有用。创建扩展提供程序。
标签: c# winforms interface controls derived-class