Visual studio VS2008 Windows窗体设计器不喜欢我的控件

Visual studio VS2008 Windows窗体设计器不喜欢我的控件,visual-studio,winforms,visual-studio-2008,windows-forms-designer,Visual Studio,Winforms,Visual Studio 2008,Windows Forms Designer,我有一个控件是这样创建的: public partial class MYControl : MyControlBase { public string InnerText { get { return textBox1.Text; } set { textBox1.Text = value; } } public MYControl() { InitializeComponent(); } } part

我有一个控件是这样创建的:

public partial class MYControl : MyControlBase
{
    public string InnerText {
        get { return textBox1.Text; }
        set { textBox1.Text = value; } 
    }
    public MYControl()
    {
        InitializeComponent();
    }
}

partial class MYControl
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(28, 61);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(7, 106);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(120, 95);
        this.listBox1.TabIndex = 1;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(91, 42);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 2;
        this.label1.Text = "label1";
        // 
        // MYControl
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.label1);
        this.Controls.Add(this.listBox1);
        this.Controls.Add(this.textBox1);
        this.Name = "MYControl";
        this.Size = new System.Drawing.Size(135, 214);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    
    private System.Windows.Forms.Label label1;
}
公共部分类MYControl:MyControlBase
{
公共字符串InnerText{
获取{return textBox1.Text;}
设置{textBox1.Text=value;}
}
公共MYControl()
{
初始化组件();
}
}
部分类控制
{
///  
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
///  
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
///  
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.textBox1=new System.Windows.Forms.TextBox();
this.listBox1=new System.Windows.Forms.ListBox();
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//文本框1
// 
this.textBox1.Location=新系统.图纸.点(28,61);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(100,20);
this.textBox1.TabIndex=0;
// 
//列表框1
// 
this.listBox1.FormattingEnabled=true;
this.listBox1.Location=新系统.图纸.点(7106);
this.listBox1.Name=“listBox1”;
this.listBox1.Size=新系统图尺寸(120,95);
this.listBox1.TabIndex=1;
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(91,42);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(35,13);
this.label1.TabIndex=2;
this.label1.Text=“label1”;
// 
//霉菌控制
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
this.Name=“MYControl”;
该尺寸=新系统图纸尺寸(135214);
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.label1;
}
MyControlBase包含ListBox和TextBox的定义。现在,当我尝试在表单设计器中查看此控件时,它会显示以下错误:

变量“listBox1”为 未申报或从未分配

变量“textBox1”为 未申报或从未分配


这显然是错误的,因为它们是在MyControlBase中通过公共访问定义的。有没有办法让窗体设计器允许我直观地编辑控件?

我想您必须使用base.listBox1base.textBox1。它们是在MyControlBase中定义的,MyControlBase是基类,而不是需要使用this关键字的子类。

不知道这是否是您的问题,但当在同一个.cs文件中定义多个类型时,设计器会遇到麻烦。如果是这种情况,请尝试为每个类使用.cs文件。

有时(总是?)VS需要您重新编译项目,然后才能在设计器中成功显示您的usercontrol

还要考虑到VS设计器实际加载并实例化控件以在窗体上显示它。您的代码实际上是在后台运行的。然而,它不会拥有它可能期望的所有东西——比如一些全局应用程序变量,甚至是同一表单上的其他东西。您的控件必须为“设计模式”做好准备。否则,如果生成异常,设计器将不显示它。每个控件上都有一个属性(不记得名称,但应该很容易找到),允许您确定该控件是否处于“设计模式”或实际运行。

编译器是正确的(通常是正确的)

源代码中未定义textbox1和listbox1。它们既不出现在派生类中,也不出现在基类中

应将以下内容添加到基类中:

protected System.Windows.Forms.TextBox textbox1; 
protected System.Windows.Forms.ListBox listbox1;
如果您决定对textbox1和listbox1使用private而不是protected,您还需要执行Nazgulled概述的更改