[英]Visual Studio changes designer generated code when I create a new class inheriting from Form
As per recommendation I ask this question in a new thread.根据建议,我在一个新线程中问这个问题。 The question stems off an answer by Reza given to this question , where I wanted custom properties to show up in the form's designer.这个问题源于 Reza 对这个问题的回答,我希望自定义属性显示在表单设计器中。
To accomplish this, I need to create a class, let's call it BaseForm
and let BaseForm
inherit from System.Windows.Forms.Form
and I should add my desired properties to this class and let my user form inherit from BaseForm
.为了实现这一点,我需要创建一个类,我们称之为BaseForm
并让BaseForm
从System.Windows.Forms.Form
继承,我应该将我想要的属性添加到这个类并让我的用户表单从BaseForm
继承。 Like this:像这样:
public partial class BaseForm : Form
{
[Browsable(true), Description("test property"), Category("Appearance")]
public Color bCol { get; set; }
}
public partial class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
}
}
When I do this, however, Visual Studio changes the designer generated partial class Form1
, where the InitializeComponent
etc. is located, to partial class BaseForm
.但是,当我这样做时,Visual Studio 将设计器生成的partial class Form1
( InitializeComponent
等所在的位置)更改为partial class BaseForm
。 This comes with the error that InitializeComponent
is not in the scope of Form1
so I change it to this:这伴随着InitializeComponent
不在Form1
范围内的错误,所以我将其更改为:
public partial class BaseForm : Form
{
[Browsable(true), Description("test property"), Category("Appearance")]
public Color bCol { get; set; }
public BaseForm()
{
InitializeComponent();
}
}
public partial class Form1 : BaseForm
{
/*public Form1()
{
InitializeComponent();
}*/
}
This is defeating the purpose of having Form1
inheriting from BaseForm
because what I see in the designer is BaseForm
which inherits from Form
, instead of Form1
which inherits from BaseForm
as I wish to do.这BaseForm
了让Form1
继承自BaseForm
的目的,因为我在设计器中看到的是继承自Form
BaseForm
,而不是我希望做的继承自BaseForm
的Form1
。
Why is Visual Studio doing this and what could I do to fix this?为什么 Visual Studio 会这样做,我可以做些什么来解决这个问题?
The designer edits the first class in your file.设计器编辑文件中的第一个类。 You have put BaseForm
as first class in your .cs file.您已将BaseForm
作为 .cs 文件中的第一类。
To solve the problem, make it in a separate file or move the codes for BaseForm
after Form1
codes.要解决此问题,请将其放在单独的文件中或将BaseForm
的代码移动到Form1
代码之后。
In general it's better to keep code of BaseForm
separate, so it's better to add new form to the project and name it BaseForm
and add additional properties to the form, then for other forms, add Inherited Form
or add new Form
and change the base class name manually.一般情况下,最好将BaseForm
代码分开,所以最好在项目中添加新表单并命名为BaseForm
并为表单添加其他属性,然后对于其他表单,添加Inherited Form
或添加新Form
并更改基类手动命名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.