简体   繁体   English

当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码

[英]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并让BaseFormSystem.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 Form1InitializeComponent等所在的位置)更改为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 ,而不是我希望做的继承自BaseFormForm1

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.

相关问题 从 WebClient 继承时 Visual Studio 类图标更改 - Visual Studio Class Icon Changes When Inheriting from WebClient Visual Studio 2008警告关于Designer生成的代码的更改 - Visual Studio 2008 Warning About Changes to Designer-Generated Code 当我使用Designer向表单添加新控件时,如何阻止visual studio更改WPF表单中控件的属性? - How to stop visual studio from changing properties of controls in WPF form when I add a new control to form using Designer? 由于MinimumSize重新打开Visual Studio窗体设计器更改布局 - Visual Studio Form Designer Changes Layout When Reopened Because Of MinimumSize 使用Visual Studio设计器时继承UserControl基类时出错 - Error inheriting UserControl base class while working with Visual Studio designer C#:当我运行程序并且Visual Studio尝试创建一个新类时,第二个表单没有加载? - C#: Second form is not loading when I run the program and Visual Studio tries to create a new class? Visual Studio中的表单设计器会覆盖我的代码 - Form designer in visual studio overrides my code 如何识别由Visual Studio的GUI设计器生成的代码? - How to recognize code generated by Visual Studio's GUI designer? Visual Studio从设计器中随机删除代码 - Visual Studio randomly deletes code from the designer 在Visual Studio(设计器视图)中重建时,会删除userControl中的更改 - Changes in my userControl are erased when I rebuild in Visual Studio (designer view)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM