qakmak
qakmak

Reputation: 1377

Why My User control not auto instance in InitializeComponent() of the Form.designer.cs?

I create a custom Textbox extend the original Textbox

 internal sealed class CustomTextBox : TextBox
    {
        private Label _label;

        internal CustomTextBox()
        {
            InitializeComponent();
            _label.Visible = true;
        }

        private void InitializeComponent()
        {
            _label = new Label();
        }

      ``    ``      
}

But after I drag it into my Form. It always can't auto generate new instance in InitializeComponent() in The Form.designer.cs

   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
            {
               // I always need to add it manually, it can't audo generate , even I manually add it and I change any other control , it will dissapeared
                this.customTextbox = new CustomTextBox(); 


                 this.SuspendLayout();

                 // this can normal generate, and will not dissapeared
                 this.Controls.Add(this.customTextbox);
                    ``           ``
                 this.ResumeLayout(false);
                 this.PerformLayout();
            }

private CustomTextBox customTextbox;

I can't understand why. Why the this.customTextbox = new CustomTextBox() can't auto generate? if I drag into this Form any other control, it will just dissapeared again.

Thank you.

Upvotes: 1

Views: 159

Answers (2)

Adriano Repetti
Adriano Repetti

Reputation: 67080

Designer cannot handle non-public members, it'll let you use your control in design surface because it parses code (constructor visibility and parameters are ignored) but it won't generate any code for that.

Your class is internal class then your constructor can safely be public.

.

Upvotes: 3

serdar
serdar

Reputation: 1618

Initializing the control is not enough. Try Controls.Add(yourCustomTextbox);.

And your custom textbox has nothing in design. You have not added the label to the controls of custom textbox as well.

Upvotes: 0

Related Questions