将自定义控件添加到设计器

时间:2014-08-03 12:48:05

标签: c# winforms custom-controls

我有一个自定义类(CustomTabControl),派生自System.Windows.Forms.TabControl,我想在设计时修改它。

在创建CustomTabControl类型的对象时,我还需要传递对另一个控件的引用。

我是怎么做到的:

我在设计时创建一个普通的TabControl,然后将引用的类型和对象修改为CustomTabControl。这是有效的,直到我有必要在CustomTabControl的构造函数中传递引用(到窗体中的另一个控件)。

似乎我无法在设计级别将参数传递给构造函数。

最初我有:

private System.Windows.Forms.TabControl tabControl1;
tabControl1 = new System.Windows.Forms.TabControl();

/*Then I change manually the type of the reference 
and of the object and this works:*/

private CustomTabControl tabControl1;
tabControl1 = CustomTabControl();

/*But If I want to pass a reference to another form's control, 
visual studio continue to removes me the creation of the object:*/

private CustomTabControl tabControl1;
tabControl1 = CustomTabControl(this.anotherFormsControl);

/*after a while the creation is removed, 
but the previous modifications of the tabControl1 still remain on the designer.cs, 
and this gives me of course an error: 
"no variable tabControl1 has been declared".
*/

1 个答案:

答案 0 :(得分:1)

如果没有对控件进行适当的设计器类修饰,Windows窗体不支持将默认构造函数添加到其他控件的控件。 要创建思想缓动器,请使用参数删除构造函数并声明一个属性以设置控件:this.anotherFormsControl to tab。

相关问题