VS2008 Windows Form Designer doesn't like my control

I have a control that is created like this:

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 contains a definition for ListBox and TextBox. Now when I try to view this control in the form designer it gives me the following errors:

The variable 'listBox1' is either undeclared or has never been assigned.

The variable 'textBox1' is either undeclared or has never been assigned.

This is clearly not true as they are defined in the publicly available MyControlBase. Is there a way to massage the Form Designer to allow me to visually edit my control?

0


source to share


4 answers


I think you will have to use base.listBox1 and base.textBox1 . They are defined in MyControlBase, which is a base class, not a child class where you need to use this keyword.



+1


source


I don't know if this is your problem, but the designer has problems when there are multiple types in the same .cs file. If so, try using a .cs file for each class.



0


source


Sometimes (always?) VS you need to recompile the project before it can successfully display your user control in the designer.

Also note that the VS designer actually loads and configures your control to show it on the form. Your code is actually running in the background. However, it won't have all the things it can expect, like some application globals or even other things in the same form. Your control must be prepared for "design mode". Otherwise, if it throws an exception, the designer won't show it. Each control had a property on it (don't remember the name, but you should find it easily) that allowed you to tell if the control was in "design mode" or actually running.

0


source


The compiler is right (as is usually the case).

Neither textbox1 nor listbox1 is specified in the source code. They do not appear in either the derived class or the base class.

You must add the following to the base class:

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

      

You will also need the changes described by Nazgulled if you choose to use private instead of protected for textbox1 and listbox1.

0


source







All Articles