Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 继承的控件将不会显示在设计器中_C#_Winforms_Inheritance_User Controls - Fatal编程技术网

C# 继承的控件将不会显示在设计器中

C# 继承的控件将不会显示在设计器中,c#,winforms,inheritance,user-controls,C#,Winforms,Inheritance,User Controls,试图在C#Winforms中创建一个从Panel继承的简单自定义控件,但当我将其更改为从“Panel”而不是“UserControl”继承时,我收到以下错误: 下面是整个类的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; us

试图在C#Winforms中创建一个从Panel继承的简单自定义控件,但当我将其更改为从“Panel”而不是“UserControl”继承时,我收到以下错误:

下面是整个类的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SETPaint
{
    public partial class Canvas : Panel
    {
        public Canvas()
        {
        InitializeComponent();
        }

        private void Canvas_Load(object sender, EventArgs e)
        {

        }
    }
}

您的代码应该是这样的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SETPaint
{
    public class Canvas : Panel
    {

    }
}

在构建解决方案后,工具箱中应该有一个名为Canvas的项

删除Designer.cs文件中的行“this.AutoScaleDimensions=…”(根据例外情况,第35行)。可能还有另一个类似于“this.AutoScaleMode=.Font”的选项

出现此问题是因为当控件从UserControl派生时使用了设计器,并且它在InitializeComponent()方法中设置了一些默认属性。这些属性是UserControl基类型的一部分,但不是Panel基类型


由于IDE设计器无法加载Designer.cs文件来解决此问题,因此您需要手动执行此操作。

抱歉,我确实有所有使用语句的语句,但没有将它们包含在原始帖子中,我将其更新为更完整的。RJ Lohan的解决方案奏效了,我只需要删除那些不起作用的行。不完全满意这是解决方案,但显然这是从特定控件继承而不是从用户控件继承时所必需的。@cArn-您的示例代码暗示Canvas控件中不会有InitializeComponent方法,但情况并非如此,并且只隐藏问题,而不是解决问题。@AdamWathan-如果您从未在设计器中打开从UserControl派生的控件,我相信IDE永远不会插入这些错误的行。