[英]Calling base class methods from InitializeComponent
我正在创建一个基于原型的表单应用程序,其中将有一个用于所有表单的基本模板。 模板中有一个方法,在构造控件时需要以实际的形式调用。
Form1.cs中:
public partial class Form1 : FormBase
{
public Form1():base()
{
InitializeComponent();
}
...
Form1.Designer.cs:
private void InitializeComponent()
{
this.lblName = new MLabel();
this.fldName = new MTextBox();
this.lblUserID = new MLabel();
this.fldUserID = new MTextBox();
this.SuspendLayout();
this.AddControl(lblName, fldName);
this.AddControl(lblUserID, fldUserID);
FormBase.cs:
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
protected void AddControl(Label lbl, Control ctrl)
{
//do something
}
...
没有编译错误,但是,当我在IDE中打开Form1设计时,显示未找到FormBase.AddControl。 即使我运行该应用程序,该方法似乎也没有被调用。
谢谢。
尝试从Form1.cs调用方法AddControl ,不要从Form1.Designer.cs调用它
请让我知道这种方式是否仍然失败。 :)
这是第二种替代方法:
只是改变这部分
protected void AddControl(Label lbl, Control ctrl)
{
//do something
}
到这个:
public void AddControl(Label lbl, Control ctrl)
{
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.