Hi, I am creating a program for my business that allows us to write quotes quickly, I am fairly new to C# and am stuck on a particular matter.

i cannot relate to my Dynamically created textbox no matter what i name it, i am using the following code to create the textbox.

TextBox field2 = new TextBox();
            field2.Location = new Point(15, 85);
            field2.Size = new Size(185, 20);
            field2.Name = "textBox2";
            field2.Visible = true;

Whenever i relate to field2 i get the error that field2 does not exist despite being created when i click the button in debug mode.

I merly want to relate to it in a button so i can store the text in a variable and need help please.

7 Years
Discussion Span
Last Post by Antenka

As I understand correctly - you're trying to access dynamically created variable field2 outside the borders of it's visibility, rather than TextBox.

To locate your dynamically created TextBox you should use this:

this.Controls["textBox2"].Text = "I'm here";

or this:

//this method returns Control[], so you should specify the index
//of interested control in that array
this.Controls.Find("textBox2", false)[0].Text = "And here";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.