【问题标题】:VS2008 Windows Form Designer does not like my controlVS2008 Windows 窗体设计器不喜欢我的控件
【发布时间】:2008-12-05 22:14:04
【问题描述】:

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

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;
}

MyControlBase 包含 ListBox 和 TextBox 的定义。现在,当我尝试在表单设计器中查看此控件时,它给了我这些错误:

变量“listBox1”是 未声明或从未分配。

变量“textBox1”是 未声明或从未分配。

这显然是错误的,因为它们在 MyControlBase 中定义为具有公共访问权限。有什么方法可以让表单设计器让我可以直观地编辑我的控件?

【问题讨论】:

    标签: visual-studio winforms visual-studio-2008 windows-forms-designer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          编译器是正确的(因为它往往是)。

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

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

          protected System.Windows.Forms.TextBox textbox1; 
          protected System.Windows.Forms.ListBox listbox1;
          

          如果您决定对 textbox1 和 listbox1 使用 private 而不是 protected,您还需要执行 Nazgulled 概述的更改。

          【讨论】:

            猜你喜欢
            • 2011-08-14
            • 2020-05-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-13
            • 2010-09-27
            • 2012-05-16
            • 1970-01-01
            相关资源
            最近更新 更多