当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码 [英] Visual Studio changes designer generated code when I create a new class inheriting from Form

查看:28
本文介绍了当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据建议,我在一个新线程中提出了这个问题.这个问题源于 Reza 对 这个问题的回答,我希望自定义属性显示在表单设计器中.

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.

为了实现这一点,我需要创建一个类,我们称之为 BaseForm 并让 BaseForm 继承自 System.Windows.Forms.Form> 并且我应该将我想要的属性添加到这个类中,并让我的用户表单从 BaseForm 继承.像这样:

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. 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();
    }
}

然而,当我这样做时,Visual Studio 将设计器生成的 partial class Form1(InitializeComponent 等所在的位置)更改为 partial class BaseForm.这伴随着 InitializeComponent 不在 Form1 范围内的错误,所以我将其更改为:

When I do this, however, Visual Studio changes the designer generated partial class Form1, where the InitializeComponent etc. is located, to partial class BaseForm. This comes with the error that InitializeComponent is not in the scope of Form1 so I change it to this:

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();
    }*/
}

这违背了让 Form1 继承自 BaseForm 的目的,因为我在设计器中看到的是继承自 BaseFormForm,而不是像我希望的那样从 BaseForm 继承的 Form1.

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.

为什么 Visual Studio 会这样做,我可以做些什么来解决这个问题?

Why is Visual Studio doing this and what could I do to fix this?

推荐答案

设计者编辑文件中的第一个类.您已将 BaseForm 作为 .cs 文件中的第一类.

The designer edits the first class in your file. You have put BaseForm as first class in your .cs file.

要解决此问题,请将其放在单独的文件中或将 BaseForm 的代码移动到 Form1 代码之后.

To solve the problem, make it in a separate file or move the codes for BaseForm after Form1 codes.

一般来说,最好将BaseForm的代码分开,所以最好在项目中添加新表单并将其命名为BaseForm,并为表单添加其他属性,然后对于其他表单,添加Inherited Form 或添加新的Form 并手动更改基类名称.

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.

这篇关于当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆