Adding controls to controls... Is this possible in an inherited class...

I'd like to add a text control via Controls.Add(Control) to the top of an existing control in a derived class.

However, when I try to add the new class, I get:

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: ArgumentException

Any ideas Perhaps this isn't possible

Code looks like:

//

// textBox1

//

textBox1 = new TextBox();

this.textBox1.Location = new System.Drawing.Point(0, 0);

this.textBox1.Size = new System.Drawing.Size(0, 20);

this.textBox1.Text = "";

this.Controls.Add(this.textBox1);




Answer this question

Adding controls to controls... Is this possible in an inherited class...

  • sjnaughton

    Thanks, gave that a try, but... it still threw the exception. I tried:

    this.textBox1.Location = new System.Drawing.Point(10, 10);

    this.textBox1.Size = new System.Drawing.Size(50, 20);

    this.textBox1.Text = "";

    Controls.Add(this.textBox1);

    Correct me if I'm wrong, but I should be able to add a control to a control

    Thanks!



  • Ameabaspy

    Oh, it was a ListBox The intrinsic controls don't allow you to add child controls to them. And this makes since because they are the smallest independent entities. You can built a house of bricks. But you can not built a house inside of a brick.
  • sharpej

    Try to have a non zero width for your textbox:

    this.textBox1.Size = new System.Drawing.Size(50, 20);


  • Mr. Lane

    Just a note, that the code does function as I can use parent to add this control to the parent (a form). It seems odd that I can't add this control to a listbox.

    Parent.Controls.Add(this.textBox1);



  • Adding controls to controls... Is this possible in an inherited class...