views:

813

answers:

4

I have a control that is created like so:

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 the definition for the ListBox and TextBox. Now when I try to view this control in the Form Designer it gives me these errors:

The variable 'listBox1' is either undeclared or was never assigned.

The variable 'textBox1' is either undeclared or was never assigned.

This is obviously wrong as they are defined in MyControlBase with public access. Is there any way to massage Form Designer into allowing me to visually edit my control?

+1  A: 

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

Nazgulled
A: 

Dunno if this is your problem, but the designer has trouble when multiple types are defined in the same .cs file. If this is the case, try using a .cs file for each class.

jlew
A: 

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

Also take into account that the VS designer actually loads up and instantiates your control to show it on the form. Your code is actually running in the background. However it will not have all the things it might expect to be there - like some global application variables or even other things on the same form. Your control has to be prepared for the "design mode". Otherwise if it generates an exception the designer will not show it. There was a property on every control (don't remember the name, but you should find it easily) that allowed you to determine if the control is in "design mode" or actually running.

Vilx-
A: 

The compiler is right (as it tends to be).

Neither textbox1 nor listbox1 are defined in the source code. They don't appear in either the derived class or the base class.

You should add the following to your base class:

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

You'll also need to do the changes outlined by Nazgulled if you decide to use private instead of protected for textbox1 and listbox1.

Esteban Brenes